ASP.NET Core 1.0动态显示模型字段

时间:2016-07-20 08:59:04

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

如果我们创建一个默认的ASP.NET Core 1.0项目,我们就有这样的默认RegisterViewModel:

 public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

假设我创建了另外两个名为FirstName的属性,LastName如何创建它以便我可以动态定义用户注册时要显示的列?示例我想显示:电子邮件,密码,ConfirmPassword和FirstName。不应显示LastName。我不是指从视图中添加/删除。

1 个答案:

答案 0 :(得分:0)

您可以在不想显示的属性上使用HiddenInput属性,如下所示:

[HiddenInput(DisplayValue = false)]
public string LastName {get; set;}