简体   繁体   中英

ASP.NET MVC - Model binding multiple parameters with same name in different entities - GET and POST different?

I was taking advantage of model binding to populate a particular parameter name in 3 separate places within an action method signature (2 filter entity parameters and a normal controller action parameter).

ie

public ViewResult foo(Entity1 something, Entity2 somethingelse, somefield)

where Entity1 and Entity2 both have somefield within them.

For one request I tested using GET and everything worked fine. For another request I used POST and only the parameter by the exact same name in the action method was populated. The rest of the parameters in the entities were mapped correctly. I tested it with another parameter, and found the same results.

Does ASP.NET MVC only populate one parameter by a particular name for POST requests, but multiple instances of a parameter by a particular parameter name for GET requests? Is this "undefined" behavior and a misuse of the model binder?

I am quite sure something else is amiss. I just gave it a test with the following

public class Foo
{  
    public string Name { get; set; }
}

public class Bar
{  
    public string Name { get; set; }
}

and as controller method:

[HttpPost]
public ActionResult Index(Foo bar, Bar foo, string Name)
{

    return View();
}

and for the view just a simple

@using (Html.BeginForm("Index", "Home")) { 

    @Html.TextBox("Name")

    <input type="submit" value="save" />
}    

and it works as expected, both bar.Name , foo.Name , and Name itself are filled with the value supplied when submitting.

So no difference between POST and GET.

To even go further into detail, this is what MVC does, where MethodInfo is an object of type System.Reflection.MethodInfo , which represents the action method about to be executed:

ParameterInfo[] parameterInfos = MethodInfo.GetParameters();
var rawParameterValues = 
        from parameterInfo in parameterInfos
        select ExtractParameterFromDictionary(parameterInfo, parameters, MethodInfo);

So for each parameter defined for the given method it will try to retrieve the parameter values from object parameters which is, amongst others, what is supplied by the querystring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM