简体   繁体   中英

Remove Prefix from ViewModel in ASP.NET MVC

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.

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

Many models to view with many partial views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM