i am new in mvc. i know this way i could handle multiple submit button in same form.
<%
using (Html.BeginForm("LoginRegistration", new { action = "LoginRegistration", controller = "Register"}, FormMethod.Post, new { @class = "form", id = "formId" }))
{
%>
//your html code here
<input type="submit" name="submitButton" value="Login"/>
<input type="submit" name="submitButton" value="Register"/>
<% }%>
[HttpPost]
public ActionResult LoginRegistration(string submitButton)
{
switch (submitButton)
{
case "Login":
return RedirectToAction("Login");
case "Register":
return RedirectToAction("Register");
default:
return null;
}
}
in my case i have to give button name submitButton for all submit button but i want to give different name to all submit button. is it possible?
i like to know is there any other way to handle this situation. so please discuss all other way with sample code to handle this situation. thanks
UPDATE
i saw that another person dealing the same issue the below way. code is given but i do not understand his trick. he add one hidden field for one submit button...why? he set button value property to true and corresponding hidden field's value property to false...why i just could not understand this trick.
another issue is when action method will be called then how bool value will be pass and which control's bool value will pass....please go through this updated code and guide me how this code works in details. thanks
<%
using (Html.BeginForm("LoginRegistration", new { action = "LoginRegistration", controller = "Register"}, FormMethod.Post, new { @class = "form", id = "formId" }))
{
%>
//your html code here
<button type="submit" name="login" value="true">Login</button>
<input type="hidden" name="login" value="false"/>
<button type="submit" name="register" value="true">Register</button>
<input type="hidden" name="register" value="false"/>
<% }%>
[HttpPost]
public ActionResult LoginRegistration(bool login, bool register)
{
if (login) return RedirectToAction("Login");
else if (register) return RedirectToAction("Register");
}
UPDATE
how my view look like
<div id="mydiv">
@using (Html.BeginForm("MultipleSubmitBtn", "MultipleSubmitBtn", FormMethod.Post, new { @Id = "Form1" }))
{
<table border="0">
<tr>
<td colspan="2">
<button type="submit" id="b1" value="true"> 1 </button>
<button type="submit" id="b2" value="true"> 2 </button>
<button type="submit" id="b3" value="true"> 3 </button>
<button type="submit" id="b4" value="true"> 4 </button>
<button type="submit" id="b5" value="true"> 5 </button>
<input type="hidden" id="b1" value="false" />
<input type="hidden" id="b2" value="false" />
<input type="hidden" id="b3" value="false" />
<input type="hidden" id="b4" value="false" />
<input type="hidden" id="b5" value="false" />
</td>
</tr>
</table>
}
</div>
controller code
public class MultipleSubmitBtnController : Controller
{
[HttpPost]
public ActionResult MultipleSubmitBtn(string b1, string b2, string b3, string b4, string b5)
{
return View();
}
}
when i use like this syntax then i got error
public ActionResult MultipleSubmitBtn(string? b1, string? b2, string b3?, string b4?, string b5?)
please check your code and tell me why it is not working. thanks