简体   繁体   English

在ASP.NET MVC中从ViewModel删除前缀

[英]Remove Prefix from ViewModel in ASP.NET MVC

I have a ViewModel class composed of several nested classes: 我有一个由几个嵌套类组成的ViewModel类:

 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 ): 由此呈现的HTML(请注意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 我希望该名称只是Email

Changing ViewData.TemplateInfo.HtmlFieldPrefix didn't help. 更改ViewData.TemplateInfo.HtmlFieldPrefix没有帮助。 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). @Html.EditorForhtmlFieldName重载将无法工作,因为我仍然希望标签和验证消息与呈现的HTML元素匹配(这些函数中的htmlFieldName不重载)。

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 要查看的许多模型具有很多局部视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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