MVCrazorforms与多个不同的提交button?

剃刀视图在表单内有3个button。 所有button的动作将需要表单值,这些值基本上是input字段的值。

每次点击任何button,它都会将我redirect到默认操作。 你可以请指导我如何可以提交表单,以不同的行动基于button按?

我真的很感激你的时间,指导和帮助。

你也可以试试这个:

 <input type="submit" name="submitbutton1" value="submit1" /> <input type="submit" name="submitbutton2" value="submit2" /> 

然后在你的默认函数中调用你想要的函数:

 if( Request.Form["submitbutton1"] != null) { // Code for function 1 } else if(Request.Form["submitButton2"] != null ) { // code for function 2 } 

这个优雅的解决scheme适用于多个提交button:

 @Html.Begin() { // Html code here <input type="submit" name="command" value="submit1" /> <input type="submit" name="command" value="submit2" /> } 

并在您的控制器的操作方法接受它作为参数。

 public ActionResult Create(Employee model, string command) { if(command.Equals("submit1")) { // Call action here... } else { // Call another action here... } } 

在视图中

 <form action="/Controller_name/action" method="Post> <input type="submit" name="btn1" value="Ok" /> <input type="submit" name="btn1" value="cancel" /> <input type="submit" name="btn1" value="Save" /> </form> 

在行动

 string str =Request.Params["btn1"]; if(str=="ok"){ } if(str=="cancel"){ } if(str=="save"){ } 

你可以使用JS + Ajax。 例如,如果你有任何button,你可以说它必须做点击事件。 这里代码:

  <input id="btnFilterData" type="button" value="myBtn"> 

这里你的button在html中:在脚本部分,你需要使用这个代码(这个部分应该在文档的末尾):

 <script type="text/javascript"> $('#btnFilterData').click(function () { myFunc(); }); </script> 

最后,你需要添加一个Ajax函数(在另一个脚本部分,应该放在文档的开头):

 function myFunc() { $.ajax({ type: "GET", contentType: "application/json", url: "/myController/myFuncOnController", data: { //params, which you can pass to yu func }, success: function(result) { error: function (errorData) { } }); }; 

我find的最干净的解决scheme如下:

这个例子是执行两个非常不同的行为; 基本前提是使用该值将数据传递给动作。

在你看来:

 @using (Html.BeginForm("DliAction", "Dli", FormMethod.Post, new { id = "mainForm" })) { if (isOnDli) { <button name="removeDli" value="@result.WeNo">Remove From DLI</button> } else { <button name="performDli" value="@result.WeNo">Perform DLI</button> } } 

然后在你的行动中:

  public ActionResult DliAction(string removeDli, string performDli) { if (string.IsNullOrEmpty(performDli)) { ... } else if (string.IsNullOrEmpty(removeDli)) { ... } return View(); } 

这个代码应该很容易改变,以实现沿着主题的变化,例如,改变button的名字是相同的,那么你只需要一个参数的动作等,如下所示:

在你看来:

 @using (Html.BeginForm("DliAction", "Dli", FormMethod.Post, new { id = "mainForm" })) { <button name="weNo" value="@result.WeNo">Process This WeNo</button> <button name="weNo" value="@result.WeNo">Process A Different WeNo This Item</button> } 

然后在你的行动中:

  public ActionResult DliAction(string weNo) { // Process the weNo... return View(); } 

你可以使用正常的button(不提交)。 使用JavaScript重写(在一个'onclick'事件)表单的“行动”属性,你想要的东西,然后提交。 使用自定义帮助程序生成button(在项目的根目录下的App_Code文件夹内创build一个“Helper.cshtml”文件)。

 @helper SubmitButton(string text, string controller,string action) { var uh = new System.Web.Mvc.UrlHelper(Context.Request.RequestContext); string url = @uh.Action(action, controller, null); <input type=button onclick="( function(e) { $(e).parent().attr('action', '@url'); //rewrite action url //create a submit button to be clicked and removed, so that onsubmit is triggered var form = document.getElementById($(e).parent().attr('id')); var button = form.ownerDocument.createElement('input'); button.style.display = 'none'; button.type = 'submit'; form.appendChild(button).click(); form.removeChild(button); } )(this)" value="@text"/> } 

然后用它作为:

 @Helpers.SubmitButton("Text for 1st button","ControllerForButton1","ActionForButton1") @Helpers.SubmitButton("Text for 2nd button","ControllerForButton2","ActionForButton2") ... 

在你的表格里面

尝试在视图中以自己的forms包装每个button。

  @using (Html.BeginForm("Action1", "Controller")) { <input type="submit" value="Button 1" /> } @using (Html.BeginForm("Action2", "Controller")) { <input type="submit" value="Button 2" /> } 

这个答案会告诉你,如何在asp.net中使用剃刀,并控制多个提交button事件。 例如,我们有两个button,一个button会将我们redirect到“PageA.cshtml”,而其他的则会将我们redirect到“PageB.cshtml”。

 @{ if (IsPost) { if(Request["btn"].Equals("button_A")) { Response.Redirect("PageA.cshtml"); } if(Request[&quot;btn"].Equals("button_B")) { Response.Redirect(&quot;PageB.cshtml&quot;); } } } <form method="post"> <input type="submit" value="button_A" name="btn"/>; <input type="submit" value="button_B" name="btn"/>; </form> 

如果你使用纯razor,即没有MVC控制器:

 <button name="SubmitForm" value="Hello">Hello</button> <button name="SubmitForm" value="World">World</button> @if (IsPost) { <p>@Request.Form["SubmitForm"]</p> } 

点击每个button应该呈现出你好和世界。

没有看到一个使用标签助手(核心MVC)的答案,所以在这里(删除操作):

在HTML上:

 <form action="" method="post" role="form"> <table> @for (var i = 0; i < Model.List.Count(); i++) { <tr> <td>@Model.List[i].ItemDescription</td> <td> <input type="submit" value="REMOVE" class="btn btn-xs btn-danger" asp-controller="ControllerName" asp-action="delete" asp-route-idForDeleteItem="@Model.List[i].idForDeleteItem" /> </td> </tr> } </table> </form> 

在控制器上:

 [HttpPost("[action]/{idForDeleteItem}"), ActionName("Delete")] public async Task<IActionResult> DeleteConfirmed(long idForDeleteItem) { ///delete with param id goes here } 

在类别声明之前,不要忘记在[Route("[controller]")]上使用[Route("[controller]")]

最简单的方法是使用html5 FormActionFormMethod

 <input type="submit" formaction="Save" formmethod="post" value="Save" /> <input type="submit" formaction="SaveForLatter" formmethod="post" value="Save For Latter" /> <input type="submit" formaction="SaveAndPublish" formmethod="post" value="Save And Publish" /> [HttpPost] public ActionResult Save(CustomerViewModel model) {...} [HttpPost] public ActionResult SaveForLatter(CustomerViewModel model){...} [HttpPost] public ActionResult SaveAndPublish(CustomerViewModel model){...} 

还有很多其他的方法可以使用,看这篇文章的ASP.Net MVC多重提交button的使用方式不同

这是为我工作。

 formaction="@Url.Action("Edit")" 

摘录:

  <input type="submit" formaction="@Url.Action("Edit")" formmethod="post" value="Save" class="btn btn-primary" /> <input type="submit" formaction="@Url.Action("PartialEdit")" formmethod="post" value="Select Type" class="btn btn-primary" /> [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit( Quote quote) { //code } [HttpPost] [ValidateAntiForgeryToken] public ActionResult PartialEdit(Quote quote) { //code } 

可以帮助一些想要使用两种不同的操作方法的人,而不是使用select器或使用客户端脚本的方法。