Reputation: 115
I have a controller as shown below. and my Login
view i have following fields as shown below.
// login model is my model class
[HttpPost]
public ActionResult Login(Login model)
{
return View();
}
@using (Html.BeginForm())
{
<p>
UserName: @Html.TextBoxFor(m=>m.UserName)
</p>
<p>
Password: @Html.PasswordFor(m => m.Password)
</p>
<p>
<input type="submit" name="login" value="Login" />
<input type="submit" name="cancel" value="Cancel" />
</p> }
I want to check in controller whether user has clicked on cancel
button or login
button. how to achieve this.
please advice thanks.
Upvotes: 0
Views: 2375
Reputation: 1355
There are the multiple ways to handle the more than one button in asp.net mvc. most commonly used is give the name and value to the input control and get the value on server side. ie. cshtml page
<input type="submit" name="save" value="Save" />
<input type="submit" name="cancel" value="Cancel" />
[HttpPost]
public ActionResult actionname(string save,string cancel)
{
string controlClicked = string.empty;
if(!string.IsNullOrEmpty(save))
{
controlClicked = "save";
}
if (!string.IsNullOrEmpty(cancel))
{
controlClicked = "cancel";
}
return View();
}
Create button and provide action name in formaction attribute.
<input type="submit" name="save" value="save" formaction="saveaction" formmethod="post" />
<input type="submit" name="cancel" value="cancel"
formaction="cancelaction" formmethod="post" />
The formaction attribute specifies where to send the form-data when a form is submitted. This attribute overrides the form's action attribute. The formaction attribute is only used for buttons with type="submit"
[HttpPost]
public ActionResult saveaction()
{
return View();
}
[HttpPost]
public ActionResult cancelaction()
{
return View();
}
Upvotes: 2