一个 Razor 视图内有 3 个按钮在一个表单内。每个按钮的操作都需要表单值,这些值基本上是来自输入字段的值。
每当我点击任何一个按钮时,它都会将我重定向到默认操作。您能否指导我如何根据按钮按下提交表单到不同的操作?
非常感谢您的时间、指导和帮助。
一个 Razor 视图内有 3 个按钮在一个表单内。每个按钮的操作都需要表单值,这些值基本上是来自输入字段的值。
每当我点击任何一个按钮时,它都会将我重定向到默认操作。您能否指导我如何根据按钮按下提交表单到不同的操作?
非常感谢您的时间、指导和帮助。
你也可以尝试这个:
<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
}
这个优雅的解决方案适用于多个提交按钮:
@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。例如,如果您有任何按钮,您可以在点击事件上告诉它该执行什么操作。以下是代码:
<input id="btnFilterData" type="button" value="myBtn">
在HTML中,你可以使用以下代码创建一个按钮:
<button>按钮</button>
在脚本部分,你需要使用以下代码(该部分应放在文档末尾):
<script>
// 在这里写你的JavaScript代码
</script>
<script type="text/javascript">
$('#btnFilterData').click(function () {
myFunc();
});
</script>
最后,您需要添加ajax函数(在另一个脚本部分中,应放置在文档的开头):
并最后,你需要添加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) {
}
});
};
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
}
对于想要使用选择器或客户端脚本而不是一个方法来拥有两个不同操作方法的人可能会有所帮助。
@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();
}
这段代码应该很容易改动以实现不同的变化,例如将按钮名称更改为相同,则只需要在操作中使用一个参数,可以看到如下:
在您的视图中:
@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();
}
@using (Html.BeginForm("Action1", "Controller"))
{
<input type="submit" value="Button 1" />
}
@using (Html.BeginForm("Action2", "Controller"))
{
<input type="submit" value="Button 2" />
}
@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")
...
在您的表单内部。
最简单的方法是使用html5 FormAction
和 FormMethod
<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 多按钮提交在不同情况下的用法
除了 @Pablo 的答案之外,对于较新版本,您还可以使用 asp-page-handler 标签助手。
在页面中:
<button asp-page-handler="Action1" type="submit">Action 1</button>
<button asp-page-handler="Action2" type="submit">Action 2</button>
然后在控制器中:
public async Task OnPostAction1Async() {...}
public async Task OnPostAction2Async() {...}
jquery
对你有帮助。 - now he who must not be named.