You can accept an input parameter with the button name and write a switch case to manually which button was click, it will have the value of clicked button.
Or can design a Action Method selector attribute as like given here to decorate appropriete action method with button selection name.
button click event are clientside only. serverside if the button has type=submit, a name and value, it will be part of the postback data. please read the w3c form post docs:
You can always use @Html.ActionLink(...) instead of buttons, pointing to some Add, Update, Delete server-side controller actions; and make the anchors look like buttons, something like this:
@Html.ActionLink("Add something", "Add", "YourController", new { someAddActionParam = someValue }, new { @class = "button-like" })
and make sure the CSS .button-like class applies a button-like style to your generated anchors.
(You would similarly call the Update and Delete methods - but be aware that the Delete should be treated differently (by using a wrapping <form> with POST method around the delete ActionLink - much safer (check this: http://stephenwalther.com/archive/2009/01/21/asp-net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx))
Ravi Kovoor
Member
8 Points
53 Posts
Button Click Event
Nov 26, 2012 07:53 PM|LINK
Hi,
I had a razor view in which there are 3 buttons. (Add, Delete, Update) as following
<button class="btn btn-primary" type="button" id="btnAdd">Add</button> <button class="btn btn-primary" type="button" id="btnUpdate">Update</button> <button class="btn btn-primary" type="button" id="btnDelete">Delete</button>
can anyone let me know how to impliment onclick function for these buttons and code should be written in controller!!!
(or) Do i need to change those 3 buttons to input type="submit" ????
CPrakash82
All-Star
18284 Points
2841 Posts
Re: Button Click Event
Nov 26, 2012 08:00 PM|LINK
Yes, if you want to do a post then convert them into input type submit.
Ravi Kovoor
Member
8 Points
53 Posts
Re: Button Click Event
Nov 26, 2012 08:15 PM|LINK
Can you give me link to a detailed example for this scenario(like 3 buttons).....? I can google it but not finding the exact one....!
CPrakash82
All-Star
18284 Points
2841 Posts
Re: Button Click Event
Nov 26, 2012 09:18 PM|LINK
You can accept an input parameter with the button name and write a switch case to manually which button was click, it will have the value of clicked button.
Or can design a Action Method selector attribute as like given here to decorate appropriete action method with button selection name.
http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx
soulwind28
Member
88 Points
19 Posts
Re: Button Click Event
Nov 26, 2012 10:08 PM|LINK
If you know and are using jQuery then you can do this.
Inside the form create a hidden field that will contain which button was clicked.
Then add the onclick event on each of your buttons.
Then add the javascript.
<script> function submitForm(btn) { $("#buttonType").val($(btn).attr('id')); $("form").submit(); } </script>When the form is submitted, the action will receive a parameter called ButtonType which can either contain "btnAdd", "btnUpdate" or "btnDelete".
Ravi Kovoor
Member
8 Points
53 Posts
Re: Button Click Event
Nov 27, 2012 01:52 PM|LINK
[MultipleButton] attribute is not loading in my mvc project!!! Do i need to any reference to it???
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: Button Click Event
Nov 27, 2012 02:12 PM|LINK
did you add the class? In what namespace?
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: Button Click Event
Nov 27, 2012 03:00 PM|LINK
button click event are clientside only. serverside if the button has type=submit, a name and value, it will be part of the postback data. please read the w3c form post docs:
http://www.w3.org/TR/html401/interact/forms.html
try the following:
<form action="doit" method="post">
<button class="btn btn-primary" type="submit" name="submit" value="btnAdd">Add</button>
<button class="btn btn-primary" type="submit" name="submit" value="btnUpdate">Update</button>
<button class="btn btn-primary" type="submit" name="submit" value="btnDelete">Delete</button>
</form>
controller public ActionResult doit(string submit) { // submit will have the value of the button }Ravi Kovoor
Member
8 Points
53 Posts
Re: Button Click Event
Nov 27, 2012 04:41 PM|LINK
Sorry, Im not getting you? what do you mean by did you add the class?
smoothcoder
Member
26 Points
3 Posts
Re: Button Click Event
Nov 27, 2012 04:57 PM|LINK
You can always use @Html.ActionLink(...) instead of buttons, pointing to some Add, Update, Delete server-side controller actions; and make the anchors look like buttons, something like this:
@Html.ActionLink("Add something", "Add", "YourController", new { someAddActionParam = someValue }, new { @class = "button-like" })and make sure the CSS .button-like class applies a button-like style to your generated anchors.
(You would similarly call the Update and Delete methods - but be aware that the Delete should be treated differently (by using a wrapping <form> with POST method around the delete ActionLink - much safer (check this: http://stephenwalther.com/archive/2009/01/21/asp-net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx))