我正在使用剃须页用于注册表格。我正在使用模型绑定将表单与页面模型属性结合。我正在使用比较(nameof())数据注释来检查密码字段。当两个密码字段匹配时,比较仍会失败,ModelState.isvalid返回false。验证摘要说"找不到名为txtPassword的属性"
这是我的表单代码:
<form method="post">
<div class="col-12">
<label asp-for="ddlInstitution"></label>
<select asp-for="ddlInstitution" asp-items="@((List<SelectListItem>)Model.Institutions)">
</select>
<div>If your institution is not listed please email the Principal Investigator to request that
it be added.</div>
</div>
<p />
<div class="row gtr-uniform">
<div class="col-6 col-12-small">
<label asp-for="txtFirstName"></label>
<input asp-for="txtFirstName" class="form-control form-control-sm" />
<span asp-validation-for="txtFirstName" class="text-danger" style="color: red"></span>
</div>
<div class="col-6 col-12-small">
<label asp-for="txtLastName"></label>
<input asp-for="txtLastName" class="form-control form-control-sm" />
<span asp-validation-for="txtLastName" class="text-danger" style="color: red"></span>
</div>
</div>
<p/>
<div class="col-12">
<label asp-for="txtEmailAddress"></label>
<input asp-for="txtEmailAddress" class="form-control form-control-sm" />
<span asp-validation-for="txtEmailAddress" class="text-danger" style="color: red"></span>
</div>
</p>
<div class="row gtr-uniform">
<div class="col-6 col-12-small">
<label asp-for="txtTitle"></label>
<input asp-for="txtTitle" class="form-control form-control-sm" />
<span asp-validation-for="txtTitle" class="text-danger" style="color: red"></span>
</div>
<div class="col-6 col-12-small">
<label asp-for="txtDepartment"></label>
<input asp-for="txtDepartment" class="form-control form-control-sm" />
<span asp-validation-for="txtDepartment" class="text-danger" style="color: red"></span>
</div>
</div>
</p>
<div class="row gtr-uniform">
<div class="col-6 col-12-small">
<label asp-for="txtPassword"></label>
<input name="txtPassword" id="txtPassword" type="password" asp-for="txtPassword" class="form-control form-control-sm" />
<span asp-validation-for="txtPassword" class="text-danger" style="color: red"></span>
</div>
<div class="col-6 col-12-small">
<label asp-for="txtConfirmPassword"></label>
<input type="password" asp-for="txtConfirmPassword" class="form-control form-control-sm" />
<span asp-validation-for="txtConfirmPassword" class="text-danger" style="color: red"></span>
</div>
</div>
<p/>
<div class="col-12">
<input type="submit" class="button fit"/>
</div>
这是我的pagemodel结合。一切似乎都正确。
[BindProperty, Required, Display(Name = "Institution Name")]
public string ddlInstitution {get; set;}
[BindProperty, Required, Display(Name="First Name")]
public string txtFirstName { get; set; }
[BindProperty, Required, Display(Name = "Last Name")]
public string txtLastName { get; set; }
[BindProperty, Required, EmailAddress, Display(Name = "Email Address")]
public string txtEmailAddress { get; set; }
public List<SelectListItem> Institutions { get; set; }
[BindProperty, Required, MinLength(2), Display(Name = "Title")]
public string txtTitle { get; set; }
[BindProperty, Required, MinLength(3), Display(Name = "Department")]
public string txtDepartment { get; set; }
[BindProperty, Required, MinLength(8), MaxLength(16), Display(Name = "Password")]
public string txtPassword { get; set; }
[BindProperty, Required, MinLength(8), MaxLength(16), Compare(nameof(txtPassword), ErrorMessage = "Your passwords do not match."), Display(Name = "Confirm Password")]
public string txtConfirmPassword { get; set; }
public string Color {get; set;}
我现在正式处于"拉出头发"阶段。一切都是正确的。感谢任何人都可以提供的任何建议。
进一步搜索时,这似乎是核心2.2中的一个错误,它将固定在核心3中。。会欣赏专家确认。