只要您在 Action 中有type
参数,我认为您不需要 javascript 来执行此操作。我假设你有这样的事情:
public ActionResult Search(string type, [other parameters])
{
....
ViewBag.SearchType = type; // put the selected type to the ViewBag
}
SelectList将selectedValue
其作为构造函数的第四个参数,因此您可以DropDownList
使用所选值在视图中轻松创建:
@Html.DropDownList("Type", new SelectList(new Dictionary<string, string> { { "Fish", "Fish" }, { "Chicken", "Chicken" }, { "Beef", "Beef" } }, "Key", "Value", ViewBag.SearchType))
当然你可以SelectList
在 Action 中创建并传递给 View:
public ActionResult Search(string type, [other parameters])
{
....
ViewBag.SearchTypeList = new SelectList(new Dictionary<string, string> { { "Fish", "Fish" }, { "Chicken", "Chicken" }, { "Beef", "Beef" } }, "Key", "Value", type); // you can assign this to the property of your ViewModel if you have one
}
然后在视图中
@Html.DropDownList("Type", ViewBag.SearchTypeList)