2

I have a ViewModel class composed of several nested classes:

 public class UserAccountViewModel : UserProfileViewModel
    {
        public UserAccountEmailViewModel UserAccountEmail { get; set; }
        public UserAccountLocationViewModel UserAccountLocation { get; set; }
        public UserAccountPasswordViewModel UserAccountPassword { get; set; }
    }

The HTML rendered from this (pay attention to model.UserAccountEmail.Email):

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

Is this:

name="UserAccountEmail.Email"

I would prefer the name to simply be Email

Changing ViewData.TemplateInfo.HtmlFieldPrefix didn't help. Overloading the htmlFieldName in @Html.EditorFor isn't going to work because I still want the label and validation message to match the rendered HTML element (no overload for htmlFieldName in these functions).

I'd prefer to not create partials for this.

Edit:

Meh...using partials actually isn't too bad. It actually makes quite a lot of sense.

1 Answer 1

4

See my answer here about this design, it might make sense for this particular strategy

Many models to view with many partial views

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

2 Comments

yeah i like the approach using partial views
@scoarescoare glad it helped, like you said, it just makes sense

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.