【发布时间】:2019-03-16 11:22:13
【问题描述】:
我正在使用 Razor 页面作为注册表单。我正在使用模型绑定将表单绑定到页面模型属性。我正在使用 Compare(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;}
我现在正式处于“拔头发”阶段。一切都是正确的。我感谢任何人提供的任何建议。
【问题讨论】:
标签: c# asp.net razor-pages