I have view and it has two button which are 'Yes' and 'No'. If I click 'Yes' redirect one page and if I click 'No' redirect to another page.
This is my view.
@using (Html.BeginForm())
{
<table >
<tr>
<td >
Ceremony :
</td>
<td>
Ceremony at @Model.ceremony_date
</td>
</tr>
<tr>
<td >
Name :
</td>
<td >
@Model.first_name @Model.middle_name @Model.last_name
</td>
</tr>
<tr>
<td colspan="2" >
@Html.Partial("_DegreeDetailsByGraduand", @Model.DegreeList)
</td>
</tr>
<tr>
<td colspan="2" >
IS information is correct ?
</tr>
<tr>
<td>
<input type="submit" id="btndegreeconfirmYes" name="btnsearch" class="searchbutton" value="Yes" />
</td> <td>
<input type="submit" id="btndegreeconfirmNo" name="btnsearch" class="searchbutton" value="No" /></td>
</tr>
</table>
}
This is my controller
[HttpPost]
public ActionResult CheckData()
{
return RedirectToRoute("detailform");
}
I don't know how to get the button value in controller. How can I do it.
Give your submit buttons a name, and then inspect the submitted value in your controller method:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>
public class MyController : Controller {
public ActionResult MyAction(string submitButton) {
switch(submitButton) {
case "Send":
// delegate sending to another controller action
case "Cancel":
// call another action to perform the cancellation
default:
// If they've submitted the form without a submitButton,
// just return the view again.
return(View());
}
}
}
Hope this helps:
I supposed you are using MVC + razor engine.
Do like this:
your 2 buttons:
<input type="submit" value="Yes" id="buttonYes"/>
<input type="submit" value="Cancel" id="buttonCancel"/>
your form:
@using (Html.BeginForm("Method", "Controller", FormMethod.Post, new { enctype = "multipart/form-data", id = "CreateReportForm" }))
{
…
}
add this javascript to your form. Adapt your action to the action on the controller that will redirect:
<script type="text/javascript">
$(document).ready(function () {
$("#buttonYes").click(function () {
$('form#CreateReportForm').attr({ action: "Controller/Create" });
});
$("#buttonCancel").click(function () {
$('form#CreateReportForm').attr({ action: " Controller /Cancel" });
});
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With