Jason Fry
Jason Fry

Reputation: 1234

Name attribute on ASP.NET MVC action

I have this action:

[HttpPost]
public ActionResult Test(Test test) 
{
    ...
}

With this class:

public class Test
{
    public string txtTest { get; set; }
}

An html form has a text field named txtTest, and I can successfully call into my action above. What property name would allow me to do this:

public class Test
{
    [SomeAttribute(Name = "txtTest")]
    public string MyTest { get; set; }
}

Upvotes: 0

Views: 922

Answers (2)

gdoron
gdoron

Reputation: 150313

Use ViewModel, don't use classes which are being used for something else.
Create ViewModel, for this specific View only.

one of the benefits is you don't need to change the names of properties the existing classes have.

You can use automapper which is highly used in MVC applications to map from the ViewModel to the Entity.

A good reading resource can be found here

Upvotes: 2

Related Questions