Asp.net mvc3剃须刀带有多个提交按钮

san*_*eep 32 asp.net validation asp.net-mvc-3

我正在使用MVC3 Razor.我在我的视图上设置了2个提交按钮,但我遇到的问题是两个提交按钮都会导致模型的验证.我想将具有特定输入控件的单个提交按钮连接起来进行验证.

小智 64

我知道这已经有几个月了,但是这里的解决方案看起来似乎很复杂,而且还没有接受的答案.如果您将输入命名为相同但为它们指定不同的值,则只需在输入中包含一个输入名称作为变量的字符串即可在控制器中获取该值.这就是我解决这个问题的方法:

视图:

 <input type="submit" id="EnterprisePush" name="btnSubmit" value="Push" />
 <input type="submit" id="EnterprisePull" name="btnSubmit" value="Pull" />
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public ActionResult EnterpriseAdmin(int id, string btnSubmit, FormCollection collection)
{
  switch (btnSubmit) {
    case "Push":
      /* Do Something here */
      break;
    case "Pull":
      /* Do Something else here */
      break;
  }
Run Code Online (Sandbox Code Playgroud)

  • 这应该标记为答案.我还发现这个解决方案适用于自定义验证. (2认同)
  • 只是为了添加解决方案,这也可以通过常规按钮完成:`<button type ="submit"name ="Command"value ="@ item [0]"> @ item [1] </ button>`然后使用`RemoteControl(string Command)`作为方法.我有一个案例,其中"命令"与我想要显示的不同. (2认同)

Bry*_*isi 30

无论您按哪个提交按钮,浏览器总是会提交整个表单.

最好的解决方案是让两个提交按钮具有相同的name属性值和不同的value属性值.

提交表单时,也会提交按钮的值.在处理表单提交的操作中,您检查按钮的值并根据该值执行正确的验证.

在你的表单中你会有这样的东西:

<button type="submit" name="Command" value="command1">Do Command #1</button>
<button type="submit" name="Command" value="command2">Do Command #2</button>
Run Code Online (Sandbox Code Playgroud)

您的表单模型如下所示:

public class MyFormModel() {
    public string Command {get;set;}
    public string SomeOtherVal {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

你的controller\action看起来像这样:

public ActionResult HandleFormSubmit(MyFormModel model) {
    if (model.Command == "command1") {
        // do something
    } else if (model.Command == "command2") {
        // do something else
    }
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*ent 21

首先,您可以通过添加CSS类"取消"来取消取消按钮上的客户端验证.请参阅:在MVC 3"取消"提交按钮中禁用客户端验证

其次,如上所述测试提交元素的表单名称,您可以使用自定义操作选择器.这是我的,我最初从评论中显示的博客文章中获取:

/// <summary>
/// Used to vary an action method based on which button in a form was pressed. This
/// is useful but is an anti-pattern because it couples the controller to names
/// used in the form elements. 
/// </summary>
/// <remarks>
/// See the example at http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx
/// </remarks>
public class AcceptButtonAttribute : ActionMethodSelectorAttribute
{
    public string ButtonName { get; set; }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        var req = controllerContext.RequestContext.HttpContext.Request;
        return !string.IsNullOrEmpty(req.Form[this.ButtonName]);
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的控制器中:

    [HttpPost]
    [ActionName("Edit")]
    [AcceptButton(ButtonName = "Cancel")]
    public ActionResult Edit_Cancel(MyModel model)
    {
        return RedirectToAction("Index");
    }

    [HttpPost]
    [AcceptButton(ButtonName = "Save")]
    public ActionResult Edit(MyModel model)
    {
        // do real work here
    }
Run Code Online (Sandbox Code Playgroud)

请注意,您需要[ActionName("Edit")]属性告诉MVC虽然使用不同的方法名称,但它适用于编辑操作.

并在您的视图中:

    <input type="submit" name="Save" value="Save" />
    <input type="submit" name="Cancel" value="Cancel" class="cancel" />
Run Code Online (Sandbox Code Playgroud)

  • 哇,这真的很干净.这不应该被标记为答案,这应该包含在MVC框架中.您应该考虑提交MVC4的拉取请求.我不在乎这是否是一个反模式,它是非常有用的! (2认同)
  • 我同意@DanVanWinkle.这在某些情况下很有用.+1 (2认同)