asp.net-mvc ASP.NET MVC:使用 Ajax.BeginForm 的多个提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18284977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: Multiple submit buttons using Ajax.BeginForm
提问by kroiz
I want to create a page that has a next button and previous button that switches the image displayed.
我想创建一个页面,它有一个切换显示图像的下一个按钮和上一个按钮。
For that purpose I created an Ajax.BeginFormand inserted into it, an image and two submit buttons.
为此,我创建了一个Ajax.BeginForm并插入其中,一个图像和两个提交按钮。
Can I (should I) have multiple submit buttons inside an Ajax.BeginForm?
我可以(应该)有多个提交按钮Ajax.BeginForm吗?
How would the controller handle each submit separately?
控制器将如何分别处理每个提交?
回答by Jaimin
Try this,
尝试这个,
View
看法
@model TwoModelInSinglePageModel.RegisterModel
@using (Ajax.BeginForm("DYmanicControllerPage", "Test", FormMethod.Post,null, new { id = "frmSignUp" }))
{
<div>
<input type="hidden" id="" name="hidden2" id="hdasd" />
@Html.HiddenFor(m => m.hidden1)
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
<br />
<div>
@Html.LabelFor(m => m.Address)
@Html.TextBoxFor(m => m.Address)
@Html.ValidationMessageFor(m => m.Address)
</div>
<br />
<div>
@Html.LabelFor(m => m.PhoneNo)
@Html.TextBoxFor(m => m.PhoneNo)
@Html.ValidationMessageFor(m => m.PhoneNo)
</div>
<input type="submit" value="Save" id="btnSave" name="ButtonType"/>
<input type="submit" value="Next" id="btnNext" name="ButtonType" />
}
Controller
控制器
[HttpPost]
public ActionResult DYmanicControllerPage(RegisterModel model, string ButtonType)
{
if(ButtonType == "Next")
{
// Do Next Here
}
if (ButtonType == "Save")
{
//Do save here
}
return JavaScript("REturn anything()");
}
回答by Sam
I would recommend that you have two buttons and then depending on what button was clicked you could set the action on the form:
我建议您有两个按钮,然后根据单击的按钮,您可以在表单上设置操作:
Razor
剃刀
$(function (){
$("#btn-prev").click(function() {
$("#form").attr
(
"action",
"@Url.Action("Action", "Controller", new {area="Area" })",
).submit();
});
$("#btn-next").click(function() {
$("#form").attr
(
"action",
"@Url.Action("Action", "Controller", new {area="Area" })",
).submit();
});
});
I am using jQuery here to do this, but I think you can get the idea.
我在这里使用 jQuery 来执行此操作,但我认为您可以理解。
回答by citronsmurf
I had the same requirement/issue and tried both solutions here and they both work for me. I LIKE the idea of setting the action via jquery when clicking so I can keep my actions separate so they can be used by other views.
我有相同的要求/问题并在这里尝试了两种解决方案,它们都对我有用。我喜欢在单击时通过 jquery 设置操作的想法,这样我就可以将我的操作分开,以便其他视图可以使用它们。
HOWEVER, I've found that when I do this while I debug, it posts TWICE and BOTHthe OnSuccess and OnFailure are triggered. It only happens when debugging though. Keep this in mind when picking.
不过,我发现,当我做到这一点,而我调试,它的帖子两次,BOTH的的onSuccess和onFailure处被触发。它只在调试时发生。挑选时请记住这一点。

