为什么我的id和name属性并不总是与我的model属性相同?

时间:2017-09-15 08:37:02

标签: c# asp.net-core asp.net-core-tag-helpers

这个问题似乎只发生在我的模型中的一个属性 UserName ...

我的模特是

public class UserModel
{
    public Guid Id { get; set; }

    [Display(Name = "Firstname")]
    public string FirstName { get; set; }

    [Display(Name = "Surname")]
    public string Surname { get; set; }

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

    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Display(Name = "Active")]
    public bool Active { get; set; }

    [Display(Name = "Active")]
    public string ActiveAsText => Active ? "Active" : "Inactive";
}

这是我在我的页面中实现的方式。

 <input asp-for="UserName" type="text" class="form-control" placeholder="Enter the username..."  required />

逻辑上,根据文档,我的id和name属性应该是我的属性名称,所以在这种情况下是UserName,但是看看id和name是......

<input id="Username" name="Username" type="text" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" placeholder="Enter the username..." required=""  value="" ng-model="pageModel.UserName" ng-class="isInputInvalid(form3E2F2153D9334BC08DFB3C68E16DF93D.UserName)">

任何人都有任何想法?

我知道我可以更改模型名称,等等等等,但是为什么问题只发生在该模型属性但EmailAddress正常工作时没有任何意义。

<input id="EmailAddress" name="EmailAddress" type="email" class="form-control ng-pristine ng-untouched ng-empty ng-valid-email ng-invalid ng-invalid-required form-control-danger" placeholder="Enter the email..." required=""  value="" ng-model="pageModel.EmailAddress" ng-class="isInputInvalid(form3E2F2153D9334BC08DFB3C68E16DF93D.EmailAddress)">

1 个答案:

答案 0 :(得分:0)

显示属性对nameid HTML属性没有影响。它用于在HTML页面中很好地显示属性。例如:

public class UserModel
{
    public Guid Id { get; set; }

    [Display(Name = "Your name")]
    public string FirstName { get; set; }

    [Display(Name = "Lastname")]
    public string Surname { get; set; }

    [Display(Name = "E-mail address")]
    public string EmailAddress { get; set; }

    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Display(Name = "Active")]
    public bool Active { get; set; }

    [Display(Name = "Active")]
    public string ActiveAsText => Active ? "Active" : "Inactive";
}
<table>
  <tr>
    <th>@Model.FirstName</th>
    <th>@Model.Surname</th> 
    <th>@Model.EmailAddress</th>
    ...
  </tr>
  ...
</table>

它将呈现为:

<table>
  <tr>
    <th>Your name</th>
    <th>Lastname</th> 
    <th>E-mail address</th>
    ...
  </tr>
  ...
</table>