Reputation: 2752
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.
Upvotes: 8
Views: 31406
Reputation: 3747
Normally I use the accepted answer, but it doesn't appear to work when you are already passing a model. And adding it as a property to the model doesn't seem to work either.
Here's another simple solution for when you are passing a model:
Add a SubmitButton property to your model
Add a hidden input with a name and id of SubmitButton
In your submit inputs, use onclick to set the hidden input value
<input type="hidden" id="SubmitButton" name="SubmitButton" value="save" />
<input type="submit" value="Clear" onclick="$('#SubmitButton').val('clear');" />
<input type="submit" value="Save" />
Upvotes: 0
Reputation: 3
You can create a handler so your controller can route your post according to the submit element name.
http://stevenbey.com/enable-multiple-submit-buttons-in-aspnet-mvc
For example, a controller can handle a submit element with the name "Action_Publish" like this:
[HttpPost, FormAction(Prefix = "Action_")]
public ActionResult Publish()
{
//...
}
Upvotes: 0
Reputation: 5967
use FormValueRequired attribute like this:
[HttpPost]
[FormValueRequired("btndegreeconfirmYes")]
public ActionResult CheckData()
{
Response.Write(submit);
return RedirectToRoute("detailform");
}
and you need to change your markup as follow:
EDIT: use two nested form
@using (Html.BeginForm())
{
@using (Html.BeginForm())
{
.
.
.
<input type="submit" id="btndegreeconfirmYes" name="btndegreeconfirmYes"class="searchbutton" value="Yes" />
}
<input type="submit" id="btndegreeconfirmNo" name="btndegreeconfirmNo" class="searchbutton" value="No" /></td>
}
by doing this submitting form with first submit button will send only its own value and then you can use it in FromValueRquired attribute.
Upvotes: 1
Reputation: 1038850
Try like this:
[HttpPost]
public ActionResult CheckData(string btnSearch)
{
if (btnSearch == "Yes") {
// The Yes submit button was clicked
} else if (btnSearch == "No") {
// The No submit button was clicked
}
return RedirectToRoute("detailform");
}
But it is usually better not to test against the text of the button but against a predefined value because the text could change and your controller action might break:
<button type="submit" name="btnsearch" value="yes">Yeah</button>
<button type="submit" name="btnsearch" value="no">Nope, I don't want to do this</button>
and then:
[HttpPost]
public ActionResult CheckData(string btnSearch)
{
if (btnSearch == "yes") {
// The Yes submit button was clicked
} else if (btnSearch == "no") {
// The No submit button was clicked
}
return RedirectToRoute("detailform");
}
And there's even a better approach where you could dispatch to a different controller action based on which button was clicked. Check this article
out.
You could have a form whose action equals to Action
:
@using (Html.BeginForm("Action", "Post"))
{
<input type="submit" name="saveDraft" value="Save Draft" />
<input type="submit" name="publish" value="Publish" />
}
and then have 2 controller actions in the corresponding controller:
public class PostController : Controller
{
[HttpParamAction]
[HttpPost]
public ActionResult SaveDraft(...)
{
// ...
}
[HttpParamAction]
[HttpPost]
public ActionResult Publish(...)
{
// ...
}
}
and here's the definition of the custom action name selector:
public class HttpParamActionAttribute : ActionNameSelectorAttribute
{
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
if (!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
Now depending on which submit button is clicked the proper controller action will be invoked.
Upvotes: 2
Reputation: 36146
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>
Upvotes: 3
Reputation: 1081
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:
Upvotes: 9