Abhijeet
Abhijeet

Reputation: 13906

ASP.NET MVC and Model Binding?

In my effort to convert few asp.net webforms into MVC razor views, I have converted all server side controls into equivalent HTML tags.

I am a bit late to realise that I should have used MVC HTML helpers, The issue here is I am not able to bind HTML tags values to strongly typed view request.

Is there any way in MVC-3 to map Model properties with HTML tags, without using HTML helpers?

Consider following Model class:
class Person
{
    public string FirstName{get; set;}
}
View
@model MyApplication.Models.Person

<input type="text" id="txtFirstName" **???**/>

In place of ??? I am expecting some attribute here to bind input to FirstName property of Model.

Is it feasible ?

Upvotes: 0

Views: 1616

Answers (1)

Lukas Kabrt
Lukas Kabrt

Reputation: 5489

The DefaultModelBinder is using the name attribute to bind values from the HTTP request to your model. If this convention doesn't suit your needs, you can write your own ModelBinder.

So in your case the following code should work (but I would recommend using HTML helpers anyway, because with HTML helper you can use automatic unobtrusive validation)

Controller:

public ActionResult Create(Person person) {
  ...
}

View:

@model MyApplication.Models.Person

<input type="text" id="txtFirstName" name="FirstName" />

Upvotes: 2

Related Questions