taazaa
taazaa

Reputation: 159

Editor Template not binding in MVC3 class

Hi I have a PersonName class that look like this:

 public class PersonName
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
    }

I have a UserCreateModel that is composed of PersonName among other properties like this:

  public class UserCreateModel
    {

        public PersonName FullName { get; set; }               
        public string UserName { get; set; }
        ...........
    }

I have a Editor template in /Views/Shared/EditorTemplates/PersonName.cshtml that looks like this (trimmed down):

@model PersonName

First: @Html.EditorFor(m => m.FirstName) 
Last: @Html.EditorFor(m => m.LastName) 
Middle: @Html.EditorFor(m => m.Middle)

However when I do (short version) :

@model UserCreateModel
 @{Html.BeginForm("Create", "User");}
        @Html.EditorForModel()  
 @{Html.EndForm();}

The PersonName does not bind to the editor and does not show up at all. I even tried UIHint, but not sure what I am missing. Also how do I debug this issue?

Please help!

Upvotes: 1

Views: 4065

Answers (3)

Beno
Beno

Reputation: 4753

you should check out this page. I think it is what you want. It is basically saying that you will have to create a custom template for the model. It is very thorough.

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

hope this helps you

Upvotes: 0

archil
archil

Reputation: 39491

If it's the exact code you use, i think you should change Model in lambda with m

    @model PersonName

First: @Html.EditorFor(m => m.FirstName) 
Last: @Html.EditorFor(m => m.LastName) 
Middle: @Html.EditorFor(m => m.Middle)

and not Model.Firstname, etc

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You have an editor template for the PersonName class (~/Views/Shared/EditorTemplates/PersonName.cshtml) but not for the UserCreateModel which is your main model. So you need to either write an editor template for the UserCreateModel class and use EditorForModel or specify the property using EditorFor like this:

@model UserCreateModel
@using(Html.BeginForm("Create", "User"))
{
    @Html.EditorFor(x => x.FullName)  
}

Upvotes: 2

Related Questions