I use this code in a view:
<button type="submit" name="SB1" value="submit1"><i class="fa fa-save"> Save</button>
<button type="submit" name="SB1" value="submit2"><i class="fa fa-update"> Update</button>
, and this code in a controller:
[HttpPost]
public ActionResult AddUser(string SB1) {
switch(SB1) {
case "submit1":
// my code..
case "submit2":
// my code..
}
}
However, SB1 string is null. also, I want use button not use input tag because I use string and fa icon in button value.
-
stackoverflow.com/questions/442704/…Aldwoni– Aldwoni2019年12月10日 07:54:22 +00:00Commented Dec 10, 2019 at 7:54
-
Does this answer your question? How do you handle multiple submit buttons in ASP.NET MVC Framework?Arijit Mukherjee– Arijit Mukherjee2019年12月10日 07:57:20 +00:00Commented Dec 10, 2019 at 7:57
-
@Aldwoni: I want use button no input tag.mishi caro– mishi caro2019年12月10日 08:05:46 +00:00Commented Dec 10, 2019 at 8:05
-
Are you sure the buttons are inside of the form tag?user8401416– user84014162019年12月10日 14:53:20 +00:00Commented Dec 10, 2019 at 14:53
4 Answers 4
[HttpPost]
public ActionResult AddUser(string SB1) {
switch(SB1) {
case "submit1":
// my code..
case "submit2":
// my code..
}
return View();
}
<form method="post" action="@Url.Action('AddUser')">
<button type="submit" name="SB1" value="submit1"><i class="fa fa-save"> Save</button>
<button type="submit" name="SB1" value="submit2"><i class="fa fa-update"> Update</button>
</form>
By this way, You will get appropriate name of button being click.
3 Comments
You can try this
<button type="button" onclick="location.href='@Url.Action("YourAction", "YourController")'"><i class="fa fa-save"></button>
YourAction: AddUser / UpdateUser
4 Comments
Doesn't a POST return either a model or a FormCollection?
[HttpPost]
public ActionResult AddUser(FormCollection form)
{
string SB1 = form.Get("SB1");
switch(SB1) {
case "submit1":
// my code..
case "submit2":
// my code..
}
return View();
}
<form method="post" action="@Url.Action('AddUser')">
<button type="submit" name="SB1" value="submit1"><i class="fa fa-save"> Save</button>
<button type="submit" name="SB1" value="submit2"><i class="fa fa-update"> Update</button>
</form>
Comments
make both a type button and on click, you can make ajax call :
<button type="button" name="SB1" onliclick=save()><i class="fa fa-save"> Save</button>
<button type="button" name="SB1" onliclick=update()><i class="fa fa-update"> Update</button>
<script>
function save() {
$.ajax({
//call for first code
});
}
function update() {
$.ajax({
//call for second code
});
}
</script>
or you can put on button click using jquery like in below URL:https://www.w3schools.com/jquery/ajax_ajax.asp
one another way you can check below link: https://www.c-sharpcorner.com/article/managing-multiple-submit-buttons-on-single-view-in-asp-net-mvc-5/