locked
How to get two fields as an ID field from dropdonwlist of razor view? RRS feed

  • Question

  • User-1651604128 posted

    I have a @Html.Dropdownlist as this:

     @Html.DropDownList("ProdidInvID", new SelectList(ViewBag.ProdData, "Prod_Inv_ID", "Product_Desc", ViewBag.SelectedProd_ID))

    The ViewBag.ProdData contains : ProductID, InvoiceID and Product_Desc,

    When the view loaded, it shows ProductDesc, when user change the data from dropdownlist, I want to pass the ProductID and InvoiceID to controller to doing some queries. and then update the other webgrid, after updating, the ProductID and InvoiceID of selected should rebind the dropdownlist to show the selected value.

    Now, the question is how to pass ProductID and InvoiceID combination to the controller? thanks in advance.

    Thursday, December 27, 2018 5:16 PM

All replies

  • User475983607 posted

    Peter Cong

    Now, the question is how to pass ProductID and InvoiceID combination to the controller? thanks in advance

    If you really need both ID values in a single input, then you must come to terms with how HTML forms work.  Form inputs have a name and a value.  If you want the select input to have two IDs then you'll need to concatenate the IDs; 1-1.  Then split the IDs in the controller.   I would update the ViewBag type by adding a property that combines the two IDs.  Unfortunately , you have not posted that bit of code so there's way to provide code.

    IMHO, the approach is wrong.  The URL should contain the invoice Id and the select should contain a list of products that go with the InvoiceId or just a list of products.  Anyway, this is a standard MVC pattern that's covered in the Getting Started tutorials.

    Thursday, December 27, 2018 6:28 PM
  • User1520731567 posted

    Hi Peter Cong,

    As @mgebhard said,you must come to terms with how HTML forms work.

    I suggest you could control your data source ViewBag.ProdData.

    You could combine ProductID and InvoiceID in action and then pass them to DropDownList.

    Best Regards.

    Yuki Tao

    Friday, December 28, 2018 9:18 AM