ASP.NET MVC: ActionNameSelector and ActionMethodSelector (or another approach to submit form to multiple action methods) – Part II


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Hope you enjoyed the first part of this blog post.

In this part I’ll discuss about how you can make your own ActionMethodSelector Attribute (though I'll use the out-of-the-box ActionNameSelectorAttribute ActionNameAttribute).

Lets first discuss the problem statement: I’ve one form which has two radio buttons and I want the form to be posted to two different Action Methods based on selection.


So here is the view:
   1: @{

   2:     ViewBag.Title = "Index";

   3: }

   4:  

   5: <h2>Index</h2>

   6:  

   7: @using (Html.BeginForm())

   8: {

   9:     @:ID: <input name="id" type="number" />

  10:     <br />

  11:     <input type="radio" value="action1" name="selector"/> @:Option1 

  12:     @:&nbsp; &nbsp; 

  13:     <input type="radio" value="action2"  name="selector" />@:Option2

  14:     <br />

  15:     <input type="submit" value="Submit" />

  16: }

  17:  

  18: @ViewBag.Msg

And here is my controller:
   1: public class HomeController : Controller

   2:  {

   3:      public ActionResult Index()

   4:      {

   5:          return View();

   6:      }

   7:  

   8:      [HttpPost, ActionName("Index")]

   9:      public ActionResult JustAnotherAction(int id)

  10:      {

  11:          ViewBag.Msg = "Message from JustAnotherAction";

  12:          return View();

  13:      }

  14:  

  15:      [HttpPost, ActionName("Index")]

  16:      public ActionResult YetAnotherAction(int id)

  17:      {

  18:          ViewBag.Msg = "Message from YetAnotherAction";

  19:          return View();

  20:      }

  21:  

  22:  

  23:  }

Now I want my form to be posted to “JustAnotherAction” when “Option1” is selected in the view and to “YetAnotherAction” when “Option2” is selected.

So for this lets make the Custom ActionMethodSelector
   1: public class CustomActionMethodSelectorAttribute:ActionMethodSelectorAttribute

   2: {

   3:     private string _name;

   4:  

   5:     public CustomActionMethodSelectorAttribute(string name)

   6:     {

   7:         this._name = name;

   8:     }

   9:   

  10:     public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)

  11:     {

  12:         string name = controllerContext.HttpContext.Request.Form["selector"];

  13:         return name != null && this._name != null &&

  14:                name.Equals(this._name, StringComparison.InvariantCultureIgnoreCase);

  15:     }

  16: }
It is very simplistic in nature. It looks for a form entry named selector, compare the value and if found a match it returns true, which means the action method on which it is applied will be selected as a potential candidate for the request.

Now apply this attribute on top of the two action methods and you are good to go.
   1: public class HomeController : Controller

   2:  {

   3:      public ActionResult Index()

   4:      {

   5:          return View();

   6:      }

   7:  

   8:      [HttpPost, ActionName("Index"), CustomActionMethodSelector("action1")]

   9:      public ActionResult JustAnotherAction(int id)

  10:      {

  11:          ViewBag.Msg = "Message from JustAnotherAction";

  12:          return View();

  13:      }

  14:  

  15:      [HttpPost, ActionName("Index"), CustomActionMethodSelector("action2")]

  16:      public ActionResult YetAnotherAction(int id)

  17:      {

  18:          ViewBag.Msg = "Message from YetAnotherAction";

  19:          return View();

  20:      }

  21:  }

Run the application and check for yourself.

Hope that helps. :)

Thanks,
Sayan

0 comments: (+add yours?)

Post a Comment