开发者

Model Binding using different names in html markup and models

开发者 https://www.devze.com 2023-04-03 14:29 出处:网络
In an asp.net MVC 1.0 application I have the following action public ActionResult Submit(ContactModel model)

In an asp.net MVC 1.0 application I have the following action

public ActionResult Submit(ContactModel model)
{
    ContentResult ret = new ContentResult();

    //do something

    return ret;
}

Where ContactModel is defined as:

public class ContactModel
开发者_如何学JAVA{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Subject { get; set; }
    public string Message { get; set; }
    public bool Authorization { get; set; }
}

Given that, in the Html Form that posts to this action, the names of the input tags are different from the names of the properties in the class ContactModel. Is there a way to map the html names to the class name so that the model binder can do its job?


The Bind Prefix (see How to use Bind Prefix?), can partially solve this problem although doesn't allow the definition of full aliases for each property. The Bind prefix has a strange behaviour though, because it assumes that between the prefix and the name there is a '.' (dot).


I don't think so.

You could create your own modelbinder though. See here for an example

0

精彩评论

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

关注公众号