Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I open my diaglog box, the dropdown list of the actual data in table is not selected. It selected the first one of data in dropdown list. Please help me.

What I have tried:

In Dialog view,

HTML
<div style="margin: 0 10px 10px 10px">
       <span style="color: #ff6a00">Menu Style</span>
       <span> @Html.DropDownListFor(modelItem => item.MenuStyle, (IEnumerable<selectlistitem>)ViewBag.MenuStyle, new { @class = "form-control", @id = ddlStyle, rows = "2", @style = "width:100%", Selected = true })</span>
  </div>

In Controller,
C#
public PartialViewResult _ShowAllMenu(int restID)
 { 
   ViewBag.MenuCategory = productRepo.MenuCategory.Where(p => p.Category == "C").Select(p => new SelectListItem { Text = p.Name, Value = p.Name });
  }
Posted
Updated 24-Aug-18 2:12am
v3

1 solution

In your controller you need to add which item is selected.
C#
ViewBag.MenuCategory = productRepo.MenuCategory.Select(p => new SelectListItem { Text = p.Name, Value = p.Name, Selected = p.Category == "C" });

And remove "Selected = true" in your @Html.DropDownListFor
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900