I have two buttons on my MVC form:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
From my Controller action how do I know which one have been pressed?
I have two buttons on my MVC form:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
From my Controller action how do I know which one have been pressed?
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
And in your controller action:
public ActionResult SomeAction(string submit)
{
if (!string.IsNullOrEmpty(submit))
{
// Save was pressed
}
else
{
// Process was pressed
}
}
Can you not find out using Request.Form Collection? If process is clicked the request.form["process"] will not be empty
I suggest looking at this post. I like the second solution since you are programming the logic in the controller and not in the view.
HTH, indyfromoz
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 it's 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();
}