本文介绍了根据单击的提交按钮将表单发布到不同的 MVC 发布操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ASP.Net MVC 4
.我在一个视图上有多个按钮.目前我正在调用相同的操作方法;我使用 name
属性来区分点击的按钮.
@using (Html.BeginForm("Submit", "SearchDisplay", new { id = Model == null ? Guid.NewGuid().ToString() : Model.SavedSearch }, FormMethod.Post)){<div class="leftSideDiv"><input type="submit" id="btnExport" class="exporttoexcelButton"name="Command" value="导出到 Excel"/>
<div class="pageWrapperForSearchSubmit"><input type="submit" class="submitButton"value="提交" id="btnSubmitChange"/>
}
//动作
[HttpPost]public ActionResult Submit(SearchCostPage searchModel, string Command){SessionHelper.ProjectCase = searchModel.ProjectCaseNumber;if (string.Equals(Command, Constants.SearchPage.ExportToExcel)){}}
问题
- 有没有办法在不同的按钮点击(没有自定义路由)上指向不同的 POST 操作方法?
- 如果没有自定义路由就没有办法,那么我们如何通过自定义路由来实现?
参考:
- Jimmy Bogard - 在 ASP.NET 中清理 POSTMVC
解决方案
最佳答案 1:
ActionNameSelectorAttribute
中提到的
怎么做您在 ASP.NET MVC 框架中处理多个提交按钮?
ASP.Net MVC 4 表单有 2 个提交按钮/操作
http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx
答案 2
参考:dotnet-tricks - 在同一个表单上处理多个提交按钮 - MVC Razor
第二种方法
添加用于处理取消按钮单击的新表单.现在,点击取消按钮,我们将发布第二个表单并重定向到主页.
第三种方法:客户端脚本
Cancel(客户端)按钮><a id="cancelUrl" href="@Html.AttributeEncode(Url.Action("Index", "Home"))"style="display:none;"></a>
I am using ASP.Net MVC 4
. I have multiple buttons on a view.. At present I am calling the same action method; and I am distinguishing the clicked button using a name
attribute.
@using (Html.BeginForm("Submit", "SearchDisplay", new { id = Model == null ? Guid.NewGuid().ToString() : Model.SavedSearch }, FormMethod.Post))
{
<div class="leftSideDiv">
<input type="submit" id="btnExport" class="exporttoexcelButton"
name="Command" value="Export to Excel" />
</div>
<div class="pageWrapperForSearchSubmit">
<input type="submit" class="submitButton"
value="Submit" id="btnSubmitChange" />
</div>
}
//ACTION
[HttpPost]
public ActionResult Submit(SearchCostPage searchModel, string Command)
{
SessionHelper.ProjectCase = searchModel.ProjectCaseNumber;
if (string.Equals(Command, Constants.SearchPage.ExportToExcel))
{
}
}
QUESTIONS
- Is there a way to direct to different POST action methods on different button clicks (without custom routing)?
- If there is no way without custom routing, how can we do it with custom routing?
References:
- Jimmy Bogard - Cleaning up POSTs in ASP.NET MVC
解决方案
BEST ANSWER 1:
ActionNameSelectorAttribute
mentioned in
How do you handle multiple submit buttons in ASP.NET MVC Framework?
ASP.Net MVC 4 Form with 2 submit buttons/actions
http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx
ANSWER 2
Reference: dotnet-tricks - Handling multiple submit buttons on the same form - MVC Razor
Second Approach
Adding a new Form for handling Cancel button click. Now, on Cancel button click we will post the second form and will redirect to the home page.
Third Approach: Client Script
<button name="ClientCancel" type="button"
onclick=" document.location.href = $('#cancelUrl').attr('href');">Cancel (Client Side)
</button>
<a id="cancelUrl" href="@Html.AttributeEncode(Url.Action("Index", "Home"))"
style="display:none;"></a>
这篇关于根据单击的提交按钮将表单发布到不同的 MVC 发布操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!