发布形式不同的MVC后行动取决于被点击提交按钮 [英] Posting form to different MVC post action depending on the clicked submit button
问题描述
我使用 ASP.Net MVC 4
。我有一个观点多个按钮..在present我打电话同样的操作方法;而我区分使用名称
属性点击的按钮。
@using(Html.BeginForm(提交,SearchDisplay,新{ID =型号== NULL Guid.NewGuid()的ToString():。Model.SavedSearch} ,FormMethod.Post))
{
< DIV CLASS =leftSideDiv>
<输入类型=提交ID =btnExport级=exporttoexcelButton
NAME =命令值=导出到Excel/>
< / DIV> < DIV CLASS =pageWrapperForSearchSubmit>
<输入类型=提交级=提交按钮
值=提交ID =btnSubmitChange/>
< / DIV>
}
//操作
[HttpPost]
公众提交的ActionResult(SearchCostPage searchModel,字符串命令)
{
SessionHelper.ProjectCase = searchModel.ProjectCaseNumber; 如果(string.Equals(指挥,Constants.SearchPage.ExportToExcel))
{ }
}
问题
- 有没有办法直接在不同的按钮点击不同的POST操作方法(没有自定义路由)?
- 如果没有办法不自定义路由,又怎能与自定义路由呢?
参考文献:
最佳答案1:
ActionNameSelectorAttribute
在
-
<一个href=\"http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework\">How你怎样对待多个在ASP.NET MVC框架提交按钮?
-
<一个href=\"http://stackoverflow.com/questions/16479946/asp-net-mvc-4-form-with-2-submit-buttons-actions\">ASP.Net MVC 4与表2的提交按钮/动作
<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx\">http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx
答2
参考:<一href=\"http://www.dotnet-tricks.com/Tutorial/mvc/cM1X161112-Handling-multiple-submit-buttons-on-the-same-form---MVC-Razor.html\">dotnet-tricks - 处理多个提交按钮相同的形式 - MVC剃刀
第二种方法
添加一个新的表格处理取消按钮点击。现在,在取消按钮点击我们将发布第二形态并重定向到主页。
第三条道路:客户端脚本
&LT;按钮名称=ClientCancel类型=按钮
的onclick =document.location.href = $('#cancelUrl')ATTR(HREF');&GT;取消(客户端)
&LT; /按钮&GT;
&LT;一个ID =cancelUrl的href =@ Html.AttributeEn code(Url.Action(指数,家))
风格=显示:无;&GT;&LT; / A&GT;
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:
BEST ANSWER 1:
ActionNameSelectorAttribute
mentioned in
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后行动取决于被点击提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!