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).
i.e.
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 particul开发者_StackOverflow社区ar 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.
精彩评论