开发者

ASP MVC 3 how to map fields from form to object?

开发者 https://www.devze.com 2023-04-11 20:05 出处:网络
I have the next form: <form method=\"POST\" action=\"test\"> 开发者_运维知识库<input type=\"text\" name=\"personFirstName\" />

I have the next form:

<form method="POST" action="test">
开发者_运维知识库<input type="text" name="personFirstName" />
<input type="text" name="personLastName" />
...
</form>

Also, I have next class:

[Serializable]
public class Person
{
 public string FirstName {get;set;}
 public string LastName {get;set;}
}

And I have the ActionResult:

public ActionResult(Person p)
{
 ...
}

So how to serialize form in Person object without rename "name" in form? Is there any alias attributes? (personFirstName -> FirstName, personLastName -> LastName)


You need to create your own custom model binder.

public class CustomModelBinder:DefaultModelBinder
{
protected override void BindProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor )
      {

      }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消