0

我有一个基本的 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> 
    }

每个按钮都有自己的形式,因为我认为这是我必须做的才能提交不同的操作?

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

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

4

1 回答 1

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();
            }

对不起,我的英语不好。

于 2013-08-12T19:10:40.500 回答