how to work on button controls in mvc asp.net -
in mvc application ,i have 3 button control on view page.i new mvc.so dont know how navigate through button control in mvc. want write 3 different methods on these 3 controls in controller. in asp.net had control event , cn write code on these events . how can achieve in mvc.
there many ways depending on trying achieve. first use standard hyperlink pointing corresponding controller action:
@html.actionlink("action 1", "actionname1", "controllername") @html.actionlink("action 2", "actionname2", "controllername") ...
which invoke respective controller actions:
public actionresult actionname1() { ... } public actionresult actionname2() { ... }
another possibility use html forms:
@using (html.beginform("actionname1", "controllername")) { <button type="submit">action 1</button> } @using (html.beginform("actionname2", "controllername")) { <button type="submit">action 2</button> } ...
a third approach have multiple submit buttons inside same form:
@using (html.beginform("actionname", "controllername")) { <button type="submit" name="a" value="action1">action 1</button> <button type="submit" name="a" value="action2">action 2</button> }
and in controller action use a
parameter used name of button know button used submit form:
public actionresult actionname(string a) { if (a == "action1") { // first button used submit form } else if (a == "action2") { // second button used submit form } else { // no button used submit form => user clicked // on enter key while inside of input fields } ... }
and if wanted dispatch different controller actions depending on button clicked write custom actionnameselectorattribute shown in this answer
.
Comments
Post a Comment