Asp.net mvc ASP.NET MVC 1自定义模型在注册页面上,确认密码未填充
我一直在使用默认的MVC1站点,在注册部分添加了一些字段,用于在注册时在数据库中创建辅助对象 我创建了视图模型,包括用户名、电子邮件、密码和密码确认属性,以及其他一些属性。然后,我将类型添加到register页面上的inherits属性中,并在controller中,将模型的新实例添加到ViewData.model属性中 问题是,当我将模型发布回服务器时,passwordConfirm属性总是空的,无论我输入什么。密码属性和所有其他属性都正确填写 要通过passwordConfirm值,是否需要执行一些特殊操作 以下是相关代码: RegistrationViewModel.cs:Asp.net mvc ASP.NET MVC 1自定义模型在注册页面上,确认密码未填充,asp.net-mvc,Asp.net Mvc,我一直在使用默认的MVC1站点,在注册部分添加了一些字段,用于在注册时在数据库中创建辅助对象 我创建了视图模型,包括用户名、电子邮件、密码和密码确认属性,以及其他一些属性。然后,我将类型添加到register页面上的inherits属性中,并在controller中,将模型的新实例添加到ViewData.model属性中 问题是,当我将模型发布回服务器时,passwordConfirm属性总是空的,无论我输入什么。密码属性和所有其他属性都正确填写 要通过passwordConfirm值,是否需要
public class RegistrationViewModel
{
public string UserName { get; set; }
public string Password { get; set; }
public string PasswordConfirm { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string HomePhone { get; set; }
public string CellPhone { get; set; }
public string OtherPhone { get; set; }
public string Twitter { get; set; }
public string Bio { get; set; }
public string DOB { get; set; }
}
AccountController.cs:
public ActionResult Register()
{
ViewData.Model = new RegistrationViewModel();
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View();
}
Register.aspx:
<% using (Html.BeginForm()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="firstname">First Name:</label>
<%= Html.TextBox("firstname", Model.FirstName) %>
<%= Html.ValidationMessage("firstname") %>
</p>
<p>
<label for="lastname">Last Name:</label>
<%= Html.TextBox("lastname", Model.LastName) %>
<%= Html.ValidationMessage("lastname") %>
</p>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username", Model.UserName) %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="email">Email:</label>
<%= Html.TextBox("email", Model.Email) %>
<%= Html.ValidationMessage("email") %>
</p>
<p>
<label for="zipcode">Zip Code:</label>
<%= Html.TextBox("zip", Model.Zip) %>
<%= Html.ValidationMessage("zip") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password", Model.Password) %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<label for="confirmPassword">Confirm password:</label>
<%= Html.Password("confirmPassword",Model.PasswordConfirm) %>
<%= Html.ValidationMessage("confirmPassword") %>
</p>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
<% } %>
帐户信息
名字:
姓氏:
用户名:
电邮:
邮政编码:
密码:
确认密码:
在视图中是confirmPassword
,在模型中是PasswordConfirm
。它尝试将名为confirmPassword的字段序列化为confirmPassword,以确认模型中的密码,该字段不在视图中,它在视图中是confirmPassword
,在模型中是PasswordConfirm
。它尝试将名为confirmPassword的字段序列化为confirmPassword,以确认模型中不存在的密码您将输入命名为confirmPassword
,但属性名为PasswordConfirm
。这些需要达成一致。将输入更改为passwordConfirm
,应该正确绑定。您将输入命名为confirmPassword
,但属性名为passwordConfirm
。这些需要达成一致。将输入更改为passwordConfirm,应该正确绑定。默认的模型绑定行为是通过将HTML元素名称与视图模型属性名称相匹配,使您的视图模型更稳定。正如前面的回答所指出的,用于捕获确认密码的输入元素名为“confirmPassword”,视图模型属性为“PasswordConfirm”。如果更新代码以使这些名称匹配,则应在模型中看到“确认密码”字符串。默认的模型绑定行为是通过将HTML元素名称与视图模型属性名称相匹配,从而对视图模型进行水合物化。正如前面的回答所指出的,用于捕获确认密码的输入元素名为“confirmPassword”,视图模型属性为“PasswordConfirm”。如果您更新代码以使这些名称匹配,您应该会在模型中看到确认密码字符串。谢谢您的快速回答。安迪得到了第一张支票。谢谢你的快速回答。安迪得到了第一张支票。