none
Selecte default value in dropdown list and pass id MVC drop-down list RRS feed

  • Question

  • i used value the model for drop down list,

    	public class Items
         {
             public int itemcategoryid { get; set; }
             public string itemcategory { get; set; }
    
         }

    and below the code for controller

    ItemViewModel catVM = new ItemViewModel();
    List<Items> catlist = catVM.GetCategoryInfo();
    ViewBag.categorylist = catlist;

    using below the code for retrieve data

        using (OracleCommand cmd = new OracleCommand("PS_Category", conn))
                        {
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            conn.Open();
                            OracleDataReader reader = cmd.ExecuteReader();
                            while (reader.Read())
                            {
                                Items listitems = new Items(); ;
                                listitems.itemcategoryid = Convert.ToInt32(reader["CATID"]);
                                listitems.itemcategory = reader["CATNAME"].ToString();
                                items.Add(listitems);
                            }
                        }
    
    

    After binding drop down list using below the HTML code, the drop down list has populated and select default name as itemcategory but did not pass id of itemcategory pass only itemcategory name for POST.

    @Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategory", "itemcategory",  Model.itemcategory))

    if i using below the HTML Code their pass value but did not select default name

    @Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategoryid", "itemcategory", Model.itemcategory))

    my model class @model Webapps8.Models.Items , itemcategory is name, and itemcategoryid is id,

    Actually i want to to select default name and pass their id how can possible it??

    Monday, November 25, 2019 11:15 AM

All replies

  • Hi,

    This case is related to C#, so this thread wil be moved to C# forum for better support.

    Best regards,

    Jeffrey


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 26, 2019 1:21 AM
  • Hi birds_Fly,

    itemcategoryid is the dataValueField and itemcategory is dataTextField.

    You should use the id field to select the default value, just modify the html code like below:

    @Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategoryid", "itemcategory", Model.itemcategoryid))

    Hope this could be helpful.

    Best Regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 27, 2019 2:16 AM
  • Wednesday, November 27, 2019 3:39 AM