Pay special attention to the following practices:
The name of the ViewBag for this method must be the same as the name of the last parameter in New SelectList (), which is the default value for the drop-down box, such as:
Viewbag.title = WebConst.UnSelect.Union (New SelectList (WebConst.Dictionary.Where (p = = P.category = "Title"). (p = = p.sort), "Value", "Display", item. Title));
The title of Viewbag.title must and
New SelectList (WebConst.Dictionary.Where (p = = P.category = "Title"). (p = = p.sort), "Value", "Display", item. Title)
The item in the. Title title has the same name, and must also be in the foreground
@Html. dropdownlistfor (p = p.title, viewbag.title as SelectList, new {@class = "Selop"}) p. Title is the same,
Otherwise, errors may occur and require special attention.
Public readonly static Ilist<selectlistitem> unselect = new list<selectlistitem>
{
New selectlistitem{text= "Please select", value= ""},
};
Public readonly static ilist<selectlistitem> YesNo = new list<selectlistitem>
{
New SelectListItem {Text = "no", Value = "false"},
New SelectListItem {Text = "yes", Value = "true"},
};
viewbag.purchaselimited = WebConst.UnSelect.Union (new SelectList (Webconst.yesno, "Value", "Text", item.) purchaselimited));
Viewbag.acceptpromotioninfo = WebConst.UnSelect.Union (new SelectList (Webconst.yesno, "Value", "Text", item.) Acceptpromotioninfo));
Viewbag.title = WebConst.UnSelect.Union (New SelectList (WebConst.Dictionary.Where (p = = P.category = "Title"). (p = = p.sort), "Value", "Display", item. Title));
Viewbag.agerange = WebConst.UnSelect.Union (New SelectList (WebConst.Dictionary.Where (p = = P.category = "Agerange") . (p = = p.sort), "Value", "Display", item. Agerange));
Front desk Use
<li>
<label class= "required" for= "title" > Appellation </label>
<div class= "Selectarea selectArea2 mln5" >
<span class= "SelectedItem" > Selection </span>
@Html. dropdownlistfor (p = p.title, viewbag.title as SelectList, new {@class = "selop"})
<em></em>
</div>
</li>
<li>
<label> Marriage No </label>
<div class= "Selectarea selectArea2 mln5" >
<span class= "SelectedItem" > Selection </span>
@Html. dropdownlistfor (p = p.maritalstatus, viewbag.maritalstatus as SelectList, new {@class = "selop"})
<em></em>
</div>
</li>
dropdownlistfor example of binding using ViewData