ASP.NET MVC Ajax.Begin form with many inputs (type=submit)
I have an Ajax form :
<%using (Ajax.BeginForm(..开发者_Go百科.)){%>
...
<input id="btn1" type="submit" value="OK1"/>
<input id="btn2" type="submit" value="OK2"/>
<%} %>
both inputs do different jobs - is it possible to catch which input has been clicked ?
Give your input buttons different names:
<input id="btn1" type="submit" name="action1" value="OK1"/>
<input id="btn2" type="submit" name="action2" value="OK2"/>
And then in the controller action check if action1
was clicked by looking at the request parameters (the name of the clicked button will be sent):
public ActionResult Index()
{
if (!string.IsNullOrEmpty(Request["action1"]))
{
// action1 was clicked
}
// ...
return View();
}
You can do this...
<input id="btnSubmit" name="pFormAction" type="submit" value="Submit" class="ym-button" /> <input id="btnSave" name="pFormAction" type="submit" value="Save" class="ym-button" /> <input type="reset" value="Clear" class="ym-button" />
and then in your repo:
[HttpPost] public ViewResult Create(string pFormAction, V2WorksheetModel pWorksheet) { }
instead of having to use different names and not taking advantage of the model binding.
I would recommend that you have two buttons and then depending on what button was clicked you could set the action on the form: I am using jQuery here to do this, but I think you can get the idea
$(function (){
$("#btn1").click(function() {
$("#form").attr
(
"action",
"@Url.Action("Action", "Controller", new {area="Area" })",
).submit();
});
$("#btn2").click(function() {
$("#form").attr
(
"action",
"@Url.Action("Action", "Controller", new {area="Area" })",
).submit();
});
});
精彩评论