在形式上ASP.NET MVC两个不同的提交按钮 [英] ASP.NET MVC two different buttons in form submit

查看:99
本文介绍了在形式上ASP.NET MVC两个不同的提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮,一个MVC格式,页面加载基本记录和用户有批准或拒绝按钮

 <按钮ID =btnAcceptAll级=BTN BTN-LG BTN-成功>&接受LT; /按钮>
 <按钮ID =btnRejectAll级=BTN BTN-LG BTN-危险>拒绝< /按钮>

我还想保留它作为一个单一的形式,因为我有很多隐藏的价值我用为好。

基本上在表单提交我想能够区分哪个按钮是pressed。

我的形式发表的那一刻。

  [ActionName(指数)]
[HttpPost]
公众的ActionResult IndexPost(QcMatchViewModel模型)
{
    //保存记录和重定向
    返回RedirectToAction(「指数」);
}


解决方案

您可以用相同的值添加名称属性来两个按钮,然后添加属性。

 <按钮ID =btnAcceptAllNAME =按钮值=接受类=BTN BTN-LG BTN-成功>接受< /按钮>
<按钮ID =btnRejectAllNAME =按钮值=拒绝级=BTN BTN-LG BTN-危险>拒绝< /按钮>

之后,在你的操作方法,你需要添加额外的按钮参数,将充满属性按钮pressed。

  [ActionName(指数)]
[HttpPost]
公众的ActionResult IndexPost(字符串键,QcMatchViewModel模型)
{
    //保存记录和重定向
    返回RedirectToAction(「指数」);
}

或者你可以把更多的按钮属性为您的视图模式。

这张贴一些额外的功能。

I have a single MVC form with two buttons, the page basically loads a record and the user has a approve or reject button

 <button id="btnAcceptAll" class="btn btn-lg btn-success">Accept</button>
 <button id="btnRejectAll" class="btn btn-lg btn-danger">Reject</button>

Id like to keep it as a single form because I have a lot of hidden value I use as well.

Basically in the Form Post I wanna be able to distinguish which button was pressed.

My Form Post at the moment..

[ActionName("Index")]
[HttpPost]
public ActionResult IndexPost(QcMatchViewModel model)
{
    //Save Record and Redirect
    return RedirectToAction("Index");
}

解决方案

You can add name attribute to both buttons with the same value and then add value attribute.

<button id="btnAcceptAll" name="button" value="accept" class="btn btn-lg btn-success">Accept</button>
<button id="btnRejectAll" name="button" value="reject" class="btn btn-lg btn-danger">Reject</button>

After that in your action method you need to add additional button parameter which will be filled with value attribute of button pressed.

[ActionName("Index")]
[HttpPost]
public ActionResult IndexPost(string button, QcMatchViewModel model)
{
    //Save Record and Redirect
    return RedirectToAction("Index");
}

Or you can put additional Button property to your view model.

Check this post for some additional features.

这篇关于在形式上ASP.NET MVC两个不同的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆