如何在html表单之外提交ASP,NET MVC列表框选择?

时间:2013-08-12 18:41:57

标签: asp.net-mvc-3 razor

我有一个基本的ASP.NET MVC3视图:

@Html.Label("Administrative Options");

    <br />

    @Html.ListBoxFor(m => m.SelectedUsers, Model.Users)

    <br />

    using (Html.BeginForm("AssignAdmins", "Account"))
    {

    <div id="MakeAdminsbtn">
        <input id="MakeAdmins" type="submit" value="Assign Administrators" onclick="return confirm('Are you sure you want to Admin?');" />
    </div> 
    }

    <br />

    using (Html.BeginForm("RevokeAdmins", "Account"))
    {

    <div id="RemAdminsbtn">
        <input id="RemAdmins" type="submit" value="Revoke Administrators" onclick="return confirm('Are you sure you want to De-Admin?');" />
    </div> 
    }

    <br />

    using (Html.BeginForm("DeleteAccounts", "Account"))
    {

    <div id="DelAcctbtn">
        <input id="DelAcct" type="submit" value="Delete Accounts" onclick="return confirm('Are you sure you want to Delete?');" />
    </div> 
    }

每个按钮都是自己的形式,因为我认为这是我为了提交不同的行为而必须做的事情?

我遇到的问题是返回的模型不包含SelectedUsers中的ListBoxFor,除非该语句位于提交按钮使用的表单内。

如何只为每个给定的提交选项使用所选用户正确填充模型,同时只显示一个ListBox?

1 个答案:

答案 0 :(得分:2)

你只能使用一个ActionMethod,然后点击女巫按钮来调用你举例说明的那个动作:

 @using (Html.BeginForm("Index", "Home"))
    {

<div>
@Html.ListBoxFor(m => m.SelectedUsers, Model.Users)

    <input id="MakeAdmins" type="submit" name="btnSubmit" value="Assign Administrators" onclick="return confirm('Are you sure you want to Admin?');" />
    <input id="RemAdmins" type="submit" name="btnSubmit" value="Revoke Administrators" onclick="return confirm('Are you sure you want to De-Admin?');" />
    <input id="DelAcct" type="submit" name="btnSubmit" value="Delete Accounts" onclick="return confirm('Are you sure you want to Delete?');" />
</div> 
    }

然后在你的控制器中

   [HttpPost]
            public ActionResult Index(string btnSubmit, FormCollection collection)
            {
//btnSubmit this is the button that is clicked.
                return View();
            }

抱歉我的英语不好。