MVC which submit button has been pressed
Name both your submit buttons the same
<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />
Then in your controller get the value of submit. Only the button clicked will pass its value.
public ActionResult Index(string submit)
{
Response.Write(submit);
return View();
}
You can of course assess that value to perform different operations with a switch block.
public ActionResult Index(string submit)
{
switch (submit)
{
case "Save":
// Do something
break;
case "Process":
// Do something
break;
default:
throw new Exception();
break;
}
return View();
}
How to detect which submit button was clicked in asp.net (mvc)
your buttons should look like this:
<button name="button" value="12">Press This</button>
<button name="button" value="13">Press That</button>
then just get them in the action
public ActionResult MyAction(string button)
{
if (button == "12"){
//Do this
}
if (button == "13"){
//Do that
}
}
how can I check which submit button was pressed?
well try binding the click event handler to the Save and SaveAndNext buttons like
$(document).delegate("#Save,#SaveAndNext","click",function(e){
console.log($(this).attr("id"));//here you will know which button was pressed
//next you can check if the form is valid or not
if($(this).closest('form').valid()){
//form is valid submit it ajax(ily)
}else{
//form is not valid
}
});
you can also cache the selector
var $this = $(this);
Determining which submit button was used to POST a form in asp.net MVC
I'd be tempted to add that I'd have used two forms, each one with a single submit button to ensure that each form only had a single responsibility. That would aid the separation of concerns and make the app more testable.
identify which button is being clicked in mvc 5
Give each input button the same name, but a different value
<input type="submit" name="action" value="save" class="btn btn-primary glyphicon glyphicon-save" />
<input type="submit" name="action" id="reset" value="reset" class="btn btn-warning active glyphicon glyphicon-refresh" />
The value will then be in your form collection as
fc["action"]
So your controller action could look like
[HttpPost]
public ActionResult insertrecd(FormCollection fc)
{
var action = fc["action"];
if(action == "save")
{
//save stuff
}
if(action =="reset")
{
//reset stuff
}
}
Checking which submit button was clicked
You should add a name field to both buttons:
<input type="submit" name="button" value="Save and continue"/>
<input type="submit" name="button" value="Save and finish"/>
Once in the controller, you can recover the element by this name field and check its value field:
String field = request.getParameter("button");
if ("Save and continue".equals(button)){
// Do stuff
}
else if ("Save and finish".equals(button)){
// Do a different stuff
}
else {
// None of them were pressed
}
Or also you can use a different name value for both buttons:
<input type="submit" name="button1" value="Save and continue"/>
<input type="submit" name="button2" value="Save and finish"/>
In your controller:
String button1 = request.getParameter("button1");
String button2 = request.getParameter("button2");
if (button1 != null){
// Do stuff
}
else if (button2 != null){
// Do a different stuff
}
else {
// None of them were pressed
}
Second solution is preferred because it doesn't depend on the value of the elements
MVC submit button is always submitting the first record details on POST
There are a couple of problems here. The first is that Html.DisplayFor
will just write the display string for the selected property's value with nothing telling the browser that it is form data to be submitted. The other is that all of your submit buttons are for the same form. There are a few ways to solve these problems. The one I would suggest is to create a separate form for each instance of DeployModel
, and create hidden inputs for each property you are expecting to receive upon submission of the form:
// inside your foreach loop
@using (Html.BeginForm("Index", "Dep", FormMethod.Post))
{
<input type="hidden" name="S1" value="@item.S1" />
<input type="hidden" name="S2" value="@item.S2" />
...
<input type="submit" value="Submit" />
}
Also please note that if you use this method you should remove the outer form - you don't want nested forms!
MVC: what code gets called when you click the submit button?
It would call whatever public action method the form action is pointing to on your controller. You can then call save on the view model.
public virtual ActionResult Save(MyViewModel model) {
model.Save();
--- more code to do stuff here
}
Set your form action to MyController/Save
You can also use using (Html.BeginForm...
in your code to point the form to a specific action method on a specific controller.
Related Topics
How to Read Connection String in .Net Core
How to Determine If a Decimal/Double Is an Integer
Add Search Functionality on Dropdownlistfor
Executing R Script Programmatically
Extending an Enum via Inheritance
How to Work with Time Zones in ASP.NET
How to Tell JSON.Net Globally to Apply the Stringenumconverter to All Enums
Simple Injector Unable to Inject Dependencies in Web API Controllers
How to Put a Task to Sleep (Or Delay) in C# 4.0
How to Implement Url Rewriting Similar to So
Simultaneous Read-Write a File in C#
How to Use Assert to Verify That an Exception Has Been Thrown
How to Update the Value Stored in Dictionary in C#
Soap Client in .Net - References or Examples