I have a dropdown menu in a GET form. When the user hits submit, they are directed to the same page and shown the form again. I would like to have the dropdown option the user displayed in the last page already selected. So for example:
我有一个GET表格的下拉菜单。当用户点击提交时,他们将被定向到同一页面并再次显示该表单。我想在已经选择的最后一页中显示用户显示的下拉选项。例如:
@Html.DropDownList("Type", null, "Type", new { @class = "sbox-input" } )
website.com/Search?Type="Beef"
website.com/Search?Type="Beef”
<select name="Type">
<option value="Fish" >Fish</option>
<option value="Chicken" >Chicken</option>
<option value="Beef" selected="selected">Beef</option>
</select>
A jQuery solution would work just as well.
jQuery解决方案也可以正常工作。
1 个解决方案
#1
1
I don't think you need javascript to do this as long as you have type
parameter in you Action. I assume that you have something like this:
只要你在Action中有类型参数,我认为你不需要javascript来执行此操作。我假设你有这样的事情:
public ActionResult Search(string type, [other parameters])
{
....
ViewBag.SearchType = type; // put the selected type to the ViewBag
}
SelectList takes selectedValue
as fourth parameter to it's constructor, so you can easy create DropDownList
in the View with the selected value:
SelectList将selectedValue作为其构造函数的第四个参数,因此您可以使用所选值在View中轻松创建DropDownList:
@Html.DropDownList("Type", new SelectList(new Dictionary<string, string> { { "Fish", "Fish" }, { "Chicken", "Chicken" }, { "Beef", "Beef" } }, "Key", "Value", ViewBag.SearchType))
Of course you can create the SelectList
in the Action and pass it to the View:
当然,您可以在Action中创建SelectList并将其传递给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
}
and then in the View
然后在视图中
@Html.DropDownList("Type", ViewBag.SearchTypeList)
#1
1
I don't think you need javascript to do this as long as you have type
parameter in you Action. I assume that you have something like this:
只要你在Action中有类型参数,我认为你不需要javascript来执行此操作。我假设你有这样的事情:
public ActionResult Search(string type, [other parameters])
{
....
ViewBag.SearchType = type; // put the selected type to the ViewBag
}
SelectList takes selectedValue
as fourth parameter to it's constructor, so you can easy create DropDownList
in the View with the selected value:
SelectList将selectedValue作为其构造函数的第四个参数,因此您可以使用所选值在View中轻松创建DropDownList:
@Html.DropDownList("Type", new SelectList(new Dictionary<string, string> { { "Fish", "Fish" }, { "Chicken", "Chicken" }, { "Beef", "Beef" } }, "Key", "Value", ViewBag.SearchType))
Of course you can create the SelectList
in the Action and pass it to the View:
当然,您可以在Action中创建SelectList并将其传递给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
}
and then in the View
然后在视图中
@Html.DropDownList("Type", ViewBag.SearchTypeList)