Handle Multiple submit Buttons
If you are working on MVC asp.net proejct and If your form has mutiple submit buttons then MVC framwork can hadle only one Action, as result you need to add a attribute to handle your button specific action.
1. Add a ActionNameSelectorAttribute derived class.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ButtonHandlerAttribute : ActionNameSelectorAttribute
{
public string MatchFormKey { get; set; }
public string MatchFormValue { get; set; }
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (string.IsNullOrEmpty(MatchFormKey) && string.IsNullOrEmpty(MatchFormValue))
{ return false; }
else if (string.IsNullOrEmpty(MatchFormKey) )
{ return actionName == MatchFormValue; }
else if (string.IsNullOrEmpty(MatchFormValue))
{ return controllerContext.HttpContext.Request[MatchFormKey] != null; }
else
{
return controllerContext.HttpContext.Request[MatchFormKey] != null &&
controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
}
}
}
2. Add submit buttons on .ascx
... your code
<p >
<input value="Register" type="submit" name="action" >
<input value="Check" type="submit" name="action" >
</p>
…your code
3. Add your attribut on your controller’s action
public class AccountController : Controller
{
.. . your code
[HttpGet]
public ActionResult Register()
{
.. . your code
}
[HttpPost]
[ButtonHandler(MatchFormKey = "action", MatchFormValue = "Register")]
public ActionResult Register(RegisterModel model)
{
//... your code
}
//your another action
[HttpPost]
[ButtonHandler(MatchFormKey = "action", MatchFormValue = "Check")]
public ActionResult Check(RegisterModel model)
{
//... your code
}
.. . your code
}
About Hasmukh patel

- Hasmukh Patel
- Harrow, London, United Kingdom
- Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.
No comments :
Post a Comment