javascript - 如何在 MVC 中发布后保留 DropDown 的值

标签 javascript jquery asp.net-mvc asp.net-mvc-4

我在 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/

相关文章:

java - 为什么 mathGPA 不能正常工作?

javascript - 清除计时器 - javascript

javascript - 我的路由 index.js 文件给出错误(找不到模块)

javascript - 如何在 jquery 链接中的选择器之后使用多个 .find()?

javascript - 本地和生产环境中 Node 的行为

javascript - (使用 JS 过滤)为什么 "#"不起作用,而字母表中的其他所有内容都起作用

javascript - 我们可以使用 Magnific Popup 插件来查看 .pdf 文件吗?

asp.net-mvc - ASP.NET MVC - 本地化路线

.net - Twitter API - 获取 GPS 点附近的推文

c# - Light在 MVC 中注入(inject)数据库上下文