http://ideasof.andersaberg.com/development/aspnet-mvc-4-model-binding-null-on-post
这里介绍的这种情况是比较操蛋的一种情况:
即、如果controller中方法参数名称,和要绑定的模型的属性同名时,就会出现对应参数始终是null的情况;
详细参见下面:
So you have your form in MVC 4 and you want to post it to a Action on a controller.
The form represents a class, let’s pretend this is a contact form and you have a a class that look like this:
class ContactMessage
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Message { get; set; }
}
And your action, looks like this:
[HttpPost]
public ActionResult Kontakt( ContactModel message)
{
var mailer = new ContactMailer();
var msg = mailer.ContactMessage(message);
msg.Send();
}
注意:上面标注红色背景色的文字,他们名字重复了,会导致绑定失败。
Well, that should work, right? Right? RIGHT? Well - No.
Because, the Model Biner in .NET MVC 4 will doubt what you mean. Because - the post value looks like this:
Name=Anders&Phone=123&Email=abc&***Message***=Hello!
You see that Message property name? That will fuck up the ModelBiner. Because… in your Action, you are waiting for the incoming property Message (it will use the name of variable). But what you really want is the full, complex, model and just calling it Message.
The solution
Do not name your incoming variables in the Action the same as you do in the model being posted. That will mess up the Model Binder.
Change your action to this:
[HttpPost]
public ActionResult Kontakt( ContactModel INCOMINGmessage)
{
var mailer = new ContactMailer();
var msg = mailer.ContactMessage(INCOMINGmessage);
msg.Send();
}