【问题标题】:asp.net vnext display name can't displayasp.net vnext显示名称无法显示
【发布时间】:2014-09-07 06:04:26
【问题描述】:

我的模特是

public class RegisterViewModel
{
    [Required]
    [Display(Name = "User name222")]
    public string UserName { get; set; }
}

我的看法是

@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })

显示的是“UserName”,而不是“User name222”。

并且ErrorMessage显示“用户名222字段为必填项。

=============================================== 所有代码都是VS14 CTP2 Asp.net Vnext Web Application自动生成。

为什么?如何解决?

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-core


    【解决方案1】:

    UserName 是您希望控制器通过该名称知道属性的 Propoerty,Dispaly 方法会将您的属性设置为该名称。在构造函数中为 Property UserName 分配一些值。

    公共类RegisterViewModel

    {
        [Required]
        [Display(Name = "UserName")]
        public string UserName { get; set; }
    
        public RegisterViewModel(RegisterViewModel model)
        {
            model.UserName = "user 123";
            this.UserName = model.UserName;
        }
    }
    

    当您在视图中调用时,请使用 html 助手中的“null”参数作为 @Html.LabelFor(m => m.UserName,null, new { @class= "col-md-2 control-label" })

    【讨论】:

    • 它还显示“用户名”,而不是“用户名222”或“用户123”
    • @user3339775 能否请您分享您的控制器以及您如何在视图中引用模型。
    【解决方案2】:

    这个问题是tracked,我们正在解决这个问题。

    谢谢, 基尔提

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2016-08-06
      • 2023-04-02
      • 1970-01-01
      • 2021-04-06
      相关资源
      最近更新 更多