我正在尝试为注册创建自定义模型,并希望将所有表单信息保存到数据库中。当我使用用户模型时,它工作,但用户模型是自动生成的模型,我不想在每次决定进行更改时重写它。所以现在当我做这个自定义寄存器模型继承自用户模型时,我尝试注册新用户时收到此错误。
这是我的注册模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using CreckJackMVC;
namespace CreckJackMVC.Models
{
public class Register : User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]//Important for auto User Increment
new public decimal cj_user_id { get; protected set; }
[Required(ErrorMessage = "please provide full name", AllowEmptyStrings = false)]
new public string cj_user_name { get; set; }
[Required(ErrorMessage = "please provide your email", AllowEmptyStrings = false)]
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
ErrorMessage = "please enter valid email")]
new public string email { get; set; }
[Required(ErrorMessage = "please provide password", AllowEmptyStrings = false)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength(50, MinimumLength = 8, ErrorMessage = "password must be 8 char long")]
new public string user_pass { get; set; }
[System.Web.Mvc.CompareAttribute("user_pass", ErrorMessage = "confirm password does not match")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
new public string confirm_user_pass { get; set; }
}
}
这是我的控制器:
public ActionResult Register(CreckJackMVC.Models.Register U)
{
if (ModelState.IsValid)
{
using (CreckJackLiveEntities dc = new CreckJackLiveEntities())
{
//you should check duplicate registration here
dc.Users.Add(U);
dc.SaveChanges();
ModelState.Clear();
U = null;
ViewBag.Message = "Registration successful! Welcome to CreakJack! ";
}
}
return View(U);
}
这里是错误消息:
答案 0 :(得分:1)
您Register
模型不应继承User
。相反,您需要一个视图模型来表示您要在视图中显示/编辑的内容(另请参阅What is viewmodel in mvc)
public class Register
{
[Required(ErrorMessage = "please provide full name")]
public string UserName { get; set; }
[Required(ErrorMessage = "please provide your email")]
[EmailAddress(ErrorMessage = "please enter valid email")]`
public string email { get; set; }
[Required(ErrorMessage = "please provide password")]
[DataType(DataType.Password)]
[StringLength(50, MinimumLength = 8, ErrorMessage = "password must be 8 char long")]
public string Password { get; set; }
[CompareAttribute("Password", ErrorMessage = "confirm password does not match")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
public string ConfirmPassword { get; set; }
}
然后在您的控制器中
[HttpPost
public ActionResult Register(Register model)
{
if (!ModelState.IsValid)
{
return View(model);
}
User user = new User()
{
UserName = model.UserName,
Password = model.Password
}
// Save the user
// Redirect to another view (e.g. the home page) - NOT return the view
}
附注:
AllowEmptyStrings = false
,因此不需要使用false
默认值[EmailAddress]
而不是自定义正则表达式(它
将生成正确的正则表达式)
您可能需要进行其他检查以确保电子邮件不存在(并且您可以使用[Remote]
属性进行客户端验证