在 MVC 4 Web 中,我有带有以下示例代码的下拉列表:
@(Html.DropDownList("Condition2", new SelectList(Model.Makes, "CCultureId", "CTitle"), "All",new {@class="span3"}))
我将全部作为选择中的第一个选项,然后按下按钮,页面显示其中的数据。回发后,按下按钮时下拉菜单被重置,你能指导我如何使下拉菜单保持其状态,即使在页面回发后(我知道在 MVC4 中没有回发,我将其作为到服务器的往返).
最佳答案
一种方法是,在您的 Controller 中,返回模型中提交的值。这意味着您的下拉列表应该连接到您的 View 模型。
View 模型:
public class MyViewModel
{
// more properties...
public string Make {get;set;}
// more properties
}
Controller :
[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
// do postback stuff
return View(model); // model.Make is set to whatever was submitted and will be returned
}
HTML:
@model Models.MyViewModel
@(Html.DropDownListFor(m => m.Make,
new SelectList(Model.Makes, "CCultureId", "CTitle", Model.Make),
"All", new {@class="span3"}))
关于c# - 在 MVC Razor 中回发后保持下拉选择状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183193/