This article briefly introduces the usage and Correction Methods of DropDownList in net mvc.
Recently, my younger brother found a bug in DropDownList in. net mvc development. The following describes the occurrence of bugs.
First, check the Controller code:
Public ActionResult Index ()
{
List <SelectListItem> country = new List <SelectListItem> ();
Country. Add (new SelectListItem () {Text = "China", Value = "1", Selected = true });
Country. Add (new SelectListItem () {Text = "", Value = "2 "});
Country. Add (new SelectListItem () {Text = "Japan", Value = "3 "});
ViewBag. country = country;
Return View ();
}
View Layer Code:
<Div>
<Span> country </span>
@ Html. DropDownList ("country", ViewBag. country as List <SelectListItem>, "select ")
</Div>
View the result:
The correct result should be China's selected status.
What is the cause? Why is it not in the selected status,
If the two names are different, the result is correct.