Control Multiple Submit Button in Asp.Net MVC

by 2:12 AM 0 comments

Input buttons in view
<p>
               <input type="submit" value="Cancel" name="action" />
               <input type="submit" value="Create" name="action" />
</p>
Add in Controller
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Person());
    }
    [HttpPost]
    [MultiButton(MatchFormKey="action", MatchFormValue="Cancel")]

    public ActionResult Cancel()
    {
        return Content("Cancel clicked");
    }
    [HttpPost]
    [MultiButton(MatchFormKey = "action", MatchFormValue = "Create")]
    public ActionResult Create(Person person)
    {
        return Content("Create clicked");
    }
}

Add Class MultiButtonAttribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request[MatchFormKey] != null &&
            controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
    }
}

Ravi Tuvar

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments:

Post a Comment