2

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.

asked Dec 10, 2019 at 7:41
4

4 Answers 4

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.

answered Dec 10, 2019 at 8:21
Sign up to request clarification or add additional context in comments.

3 Comments

I use this and send null SB1 value.
@mishicaro, i don't know but its tottaly worked here, even i create sample project for this issue which is work as expected related to you're requirement.
i don't know why, but let me try to upload same thing on other source so that we both can verify this functionality
2

You can try this

<button type="button" onclick="location.href='@Url.Action("YourAction", "YourController")'"><i class="fa fa-save"></button>

YourAction: AddUser / UpdateUser

answered Dec 10, 2019 at 7:51

4 Comments

I want use one action not multi action, so use Url.Action("YourAction", "YourController", new {SB1 = "submit1"}) in onclick event button. but send null SB1 in action.
So you need to use Ajax: c-sharpcorner.com/blogs/…
Is there a solution other than Ajax?
You should separate into 2 actions to follow SRP principle: deviq.com/single-responsibility-principle
2

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>
answered Dec 13, 2019 at 5:24

Comments

2

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/

ankitkanojia
3,1224 gold badges24 silver badges37 bronze badges
answered Dec 10, 2019 at 7:50

2 Comments

I want use one action in controller and not use ajax for submit
Go with this solution is it ok :c-sharpcorner.com/article/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.