Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get button value to controller?

Tags:

c#

asp.net-mvc

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.

like image 911
aruni Avatar asked Oct 11 '12 09:10

aruni


2 Answers

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:

like image 131
Mohan Avatar answered Sep 19 '22 04:09

Mohan


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>
like image 39
Diego Avatar answered Sep 21 '22 04:09

Diego