0

I have the classes:

public class PersonDetailsModel
{
    public string FistName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public string Email
    {
        get;
        set;
    }
}

public class RegisterCoupleModel
{
    public PersonDetailsModel Groom
    {
        get;
        set;
    }

    public PersonDetailsModel Bride
    {
        get;
        set;
    }


    public string UrlKeyword
    {
        get;
        set;
    }

    public string ReCaptcha
    {
        get;
        set;
    }
}

folder Shared > EditorTemplates

PersonDetailsModel.cshtml

@model BindSolution.AndMarried.ViewModel.PersonDetailsModel

<div class="editor-label">
    @Html.LabelFor(m => m.FistName)
</div>
<div class="editor-field">     
    @Html.EditorFor(m => m.FistName)
    @Html.ValidationMessageFor(m => m.FistName)
</div>

<div class="editor-label">
    @Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">     
    @Html.EditorFor(m => m.LastName)
    @Html.ValidationMessageFor(m => m.LastName)
</div>

<div class="editor-label">
    @Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">     
    @Html.EditorFor(m => m.Email)
    @Html.ValidationMessageFor(m => m.Email)
</div>

In my View:

@Html.EditorForModel()

Only the UrlKeyword and ReCapcha fields are displayed!

Why Asp.net MVC not use templantes in shared folder to display my nested type PersonDetailsModel ?

1 Answer 1

1

You know, you could have just edited your other question. :)

Anyway, i don't think that will work. You either need to control the entire template yourself, or let MVC do it all itself. (i could be wrong)

Create an editor template for RegisterCoupleModel:

@model BindSolution.AndMarried.ViewModel.RegisterCoupleModel

@Html.EditorFor(model => model.Groom)
@Html.EditorFor(model => model.Bride)
@Html.EditorFor(model => model.UrlKeyword)
@Html.EditorFor(model => model.ReCapcha)

Or you could use a [UIHint("PersonDetailsModel"] attribute on your Groom and Bride properties in your ViewModel.

Read up on the remarks section in EditorForModel for how this all works.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought the context was different:) Am I required to create the template RegisterCoupleModel? It could not just create the template PersonDetailsModel? I tried to create the template and put the attribute UIHint("PersonDetailsModel") but did not work.
@Riderman - yes, create the template RegisterCoupleModel, in addition to the PersonDetailsModel one, and it should work. (no need for the UIHint in this case). Html.EditorForModel in your main view will render the RegisterCoupleModel template, which will then call PersonDetailsModel template for Bride and Groom.
Ok, I thought it was not necessary creating the template RegisterCoupleModel. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.