Can you explain to me the Action parameters for MVCs Controller methods?

Can someone explain or link to an article that explains how the parameters passed to the controller action are populated? I understand the basic mapping when you have a Controller / Action / ID and the ID is passed as a variable, if it is not convertible to the type you are requesting then it will not be passed to that action.

However, I was looking at the MVCContrib subcontroller code and there was the following example:

public ActionResult Index(FirstLevelSubController firstLevel)

      

I want to know how this parameter is filled in because as far as I know nothing is being passed to fill this?

Let's say I created the following activity, which is the only activity in the controller:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(Task task, ToDoList list)

      

What should I return and why? (I know I can do a quick test to see if they came back, but that would make me no wiser about why.

thank

+1


source to share


2 answers


Typically, this is the ControllerActionInvoker function. Objects are passed to the action after they are created in a custom ControllerActionInvoker implementation. For more information on how ControllerActionInvoker works, see ASP.NET MVC - ControllerActionInvoker: Part 1



There is also a function called Model Binding that helps you bind query parameters to your objects, so in most cases you don't need to create your own ControllerActionInvoker implementation. Take a look at this: ASP.NET MVC Model Binding by ScottGu

+2


source


First of all, the only way to get the parameters passed to the method with the [AcceptVerbs (HttpVerbs.Get)] attribute are request parameters. Example:

http://localhost/Task/Index/?task=mytask&todolist=a,b,c,d

      

Many of the action methods you see with complex parameters are called through a column and are likely to be candidates for [AcceptVerbs (HttpVerbs.Post)].

Create the following example:

public ActionResult Index(int id, FormCollection form)
{

}

      

If you need to validate a collection of forms, after posting the form, you can have this:

form["name"] = "bob"
form["city"] = "LA"
form["state"] = "CA"
form["zip"] = "90210"

      



in this case asp.net mvc just parsed the form values ​​and threw them into the form collection object. Of course, now you have to extract each parameter manually to access the values. Wouldn't it be nice if there was a way that asp.net mvc would handle this process for you? If you had the following class:

public class User
{
    string string Name {get; set;}
    string string City {get; set;}
    string string State {get; set;}
    string string Zip {get; set;}
}

      

and added to your action method

public ActionResult Index(int id, User user)

      

asp.net mvc will use reflection to pop up the custom parameter before calling your action method. Basically, for each key in the collection of forms, it does the following: It uses reflection to find that property in the list of parameters declared in your action method. If it finds a property that matches it, it will try to set its value.

user.Name = form["name"]
user.City = form["city"]
user.State = form["state"]
user.Zip = int.Parse(form["zip"])

      

However, the id value is set from the routing values ​​as opposed to the form collection.

+4


source







All Articles