我在 MVC 中有 3 个下拉列表,选择它们的值并单击下一步按钮后,它将发布数据并在页面底部显示一些带有值的文本框。当我单击 NEXT 并转到 POST 方法时,我收到错误,因为没有 ViewBag 值。如何在Post方法中保留viewbag的值?
这是我的索引 Controller 方法:
public ActionResult Index()
{
DAL.DataManager data = new DAL.DataManager();
List<LALegalType> LegalData = new List<LALegalType>();
//------------------------------Populate Entity Dropdown------------------------------------------//
LegalData = data.get_LA_Get_Legal_Type_Xref("Entity");
List<SelectListItem> listItem = createDropdowns(LegalData);
ViewBag.DDLEnityValue = new SelectList(listItem, "Value", "Text");
...
return View();
这是我的观点:
@Html.DropDownList("Entity", (SelectList)ViewBag.DDLEnityValue, new
{
@class = "validate[required]",
tabindex = 1,
required = true
})
}
这是 mt post 方法:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formCollection, Compliance_FormField model)
{
Utilities.functions F = new Utilities.functions();
string Entity = F.getControlValue(Request["Entity"]);
.....
我的实体这里确实有一些下拉列表而不是值。
最佳答案
如果帖子无法将用户导航到新页面,那么您必须重新绑定(bind)帖子操作方法上的下拉列表。 MVC 没有 ViewState 来保存控件的状态,因此需要手动维护状态。
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formCollection, Compliance_FormField model)
{
Utilities.functions F = new Utilities.functions();
string Entity = F.getControlValue(Request["Entity"]);
//Rebind DDL's
//Add logic to rebind you dropdownlist and set selected item from you model
}
希望这有帮助!
关于javascript - 如何在 MVC 中发布后保留 DropDown 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33506412/