Pavel Voronin
Pavel Voronin

Reputation: 13985

Is it possible to override names of model bound elements with attributes?

As I understandd MVC framework html helpers (TextBoxFor, EditorFor) use reflection for defining the names of input elements to provide reverse binding to the model. fo I got used to rather long identifier names for the sake of clarity: FirstName, LastName and many other examples.

Do standard helpers support any attribute whcih allow abstract from model member names?

For XML serialization we have attributes XmlElement, XmlAttribute where we can set the name of the element.

Thus I need something like this:

public class Person
{
    [Name("fn")]
    public string FirtsName {get;set;}

    [Name("ln")]
    public string LastName {get;set;}
}

and now call to Html.TextBoxFor( model => model.FirstName) should give <input id="fn" name="fn" type="text" value="" />

Upvotes: 0

Views: 172

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Do standard helpers support any attribute whcih allow abstract from model member names?

No, they don't. You could write a custom model binder or simply name the properties of the view model as expected.

Upvotes: 2

Related Questions