MVC 3 添加的字段多于提供给帐户控制器的字段
Posted
技术标签:
【中文标题】MVC 3 添加的字段多于提供给帐户控制器的字段【英文标题】:MVC 3 add more fields then provided to account controler 【发布时间】:2013-04-07 23:33:58 【问题描述】:有人可以帮帮我吗? 我想向帐户控制器添加比提供的更多的字段。我也想在表格中查看这些字段。我在 Register 类中添加字段。而且我不确定我想使用自动增量的 ID 字段,但不知道如果我没有看到表格怎么办。在普通数据库中它会自动执行。谢谢。 我的 帐户模型:
公共类 ChangePasswordModel [必需的] [数据类型(数据类型。密码)] [显示(名称=“当前密码”)] 公共字符串 OldPassword 获取;放;
[Required]
[StringLength(100, ErrorMessage = "The 0 must be at least 2 characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword get; set;
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword get; set;
public class LogOnModel
[Required]
[Display(Name = "User name")]
public string UserName get; set;
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password get; set;
[Display(Name = "Remember me?")]
public bool RememberMe get; set;
public class RegisterModel
[Required]
public int ID get;
[Required]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
public string FirstName get; set;
[Required]
[DataType(DataType.Text)]
[Display(Name = "Last Name")]
public string LastName get; set;
[Required]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone")]
public string Phone get; set;
[Required]
[DataType(DataType.Text)]
[Display(Name = "First line of address")]
public string Address get; set;
[DataType(DataType.Date)]
[Display(Name = "DOB => dd/mm/yyyy")]
public DateTime DateOfBirth get; set;
[Required]
[PostCode(ErrorMessage= "PostCode is not valid")]
[Display(Name = "Post Code")]
public string PostCode get; set;
[Required]
[Display(Name = "User name")]
public string UserName get; set;
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email get; set;
[Required]
[StringLength(100, ErrorMessage = "The 0 must be at least 2 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;
public class PostCodeAttribute : RegularExpressionAttribute
public PostCodeAttribute()
: base(
@"([Gg][Ii][Rr] 0[Aa]2)|((([A-Za-z][0-9]1,2)|(([A-Za-z][A-Ha-
hJ-Yj-y][0-9]1,2)|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-
Yj-y][0-9]?[A-Za-z])))) 0,1[0-9][A-Za-z]2)")
ErrorMessage = "PostCode is invalid";
然后在 AccountController 中,我可以创建在表中具有相同数据字段的用户,但 Membership 没有此选项 Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
【问题讨论】:
【参考方案1】:这是“经典”的 ASP.NET,但应该会有所帮助
http://www.asp.net/web-forms/tutorials/security/membership/storing-additional-user-information-cs
(从这里adding more fields to registration form using membership on mysql and MVC 3)
【讨论】:
【参考方案2】:您可能需要考虑SimpleMembership(这是未来)并使用您自己的架构,然后使用 NuGet 到use it with MVC 3。
【讨论】:
以上是关于MVC 3 添加的字段多于提供给帐户控制器的字段的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.NET MVC5 中传递给控制器时,DateTime 字段为空
如何在 ASP.NET MVC 中执行辅助操作(即计算字段)?