previously I have hyperlink to call the GetBackQuestion Mehtod of GetQuestion Controller. Now i got a requirement to convert it into button. so if i add the button its calling the default form submit method of
SaveCheckBox method.
so how do i call the different methods based on clicked button.
Now handle the button clicks using JQuery click handler update the Hidden variable value (btn1/btn2) and in controller read the Hidden variable value. This value tells which button is clicked on the page i.e.,
Chakradhar M...
Member
41 Points
47 Posts
calling controller method based on the button clicked.
Apr 19, 2012 11:44 AM|LINK
Hi ,
I have this view.
<div data-role="fieldcontain"> @using (Ajax.BeginForm("SaveCheckbox", "GetQuestion", new { QuestionNo = Model.QuestionNo, PreviousQuestionNo = Model.PreviousQuestion }, new AjaxOptions { }, new { id = "myform_" + @Model.QuestionNo })) { @Html.AntiForgeryToken() <fieldset data-role="controlgroup" id="@(Model.QuestionNo)_fld"> <label class="select">@Model.QuestionText </label> <br /> @foreach (var item in Model.Options) { <input type="checkbox" name="selectedObjects" id="@(Model.QuestionNo)_@(item.OptionNumber)_chk" value="@item.OptionNumber" class="custom" /> <label for="@(Model.QuestionNo)_@(item.OptionNumber)_chk"> @item.OptionText</label> } <br /> </fieldset> <div class="ui-grid-a"> <div data-role="none" style="float: left;"> @if (@Model.IsFirst == false && @Model.ShowPrevousButton == true) { @*<a href='@Url.Action("GetBackQuestion", "GetQuestion", new { QuestionNo = Model.QuestionNo }, null)'> @Model.BackButtonText</a>*@ <input type="submit" id="@(Model.QuestionNo)_chk" data-inline="true" value="@Model.BackButtonText"/> } </div> <div data-role="none" style="float: right;"> <input type="submit" id="@(Model.QuestionNo)_chk" data-inline="true" onclick="return SaveCheckBoxNew(this);" value="@Model.SubmitButtonText"/> </div> </div> } </div>previously I have hyperlink to call the GetBackQuestion Mehtod of GetQuestion Controller. Now i got a requirement to convert it into button. so if i add the button its calling the default form submit method of SaveCheckBox method.
so how do i call the different methods based on clicked button.
Thanks.
mm10
Contributor
6395 Points
1182 Posts
Re: calling controller method based on the button clicked.
Apr 19, 2012 12:18 PM|LINK
Use an input button to make a redirect to your action:
@*<input type="button" value='@Model.BackButtontext' onClick="location.href='@Url.Action("GetBackQuestion", "GetQuestion", new { QuestionNo = Model.QuestionNo }, null)'">*@ashok4v
Member
220 Points
58 Posts
Re: calling controller method based on the button clicked.
Apr 19, 2012 02:07 PM|LINK
create a hidden field on your page as follows
@Html.Hidden("hdnWhichButton")
Now handle the button clicks using JQuery click handler update the Hidden variable value (btn1/btn2) and in controller read the Hidden variable value. This value tells which button is clicked on the page i.e.,
if(hdnWhichButton == "btn1")
// button1 is clicked
else
//button2 is clicked
Thanks,
Ashok
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)