asp.net mvc 3 dataanotation在模型中的数据库字段外部未验证

时间:2011-03-20 10:40:22

标签: asp.net-mvc asp.net-mvc-3 data-annotations

我如何使这项工作就像我的数据库表中有5个字段一样。

  1. 我使用datannotations创建了一个验证模型,并创建了compare password字段以及comparepassword属性。 (现在我的模型有6个字段,包括一个外部字段比较密码和5个数据库字段)

  2. 我使用该模型创建了一个视图。(用户)

  3. 现在我也创建了一个控制器,但问题出在这里,当调用ActionMethod编译器抱怨外部字段并告诉它找不到任何扩展方法时。如何创建扩展方法或解决此错误?

    [HttpPost]
    public ActionResult Create(User Users)
    {
    
    }
    
  4. 这是我得到的错误:

    CS1061:'MvcApplication1.Models.User'不包含'ComparePassword'的定义,并且没有可以找到接受类型'MvcApplication1.Models.User'的第一个参数的扩展方法'ComparePassword'(你是否错过了使用指令或程序集引用?)

    我的模特:

    [MetadataType(typeof(UserModel))]
    public partial class User { }  
    public class UserModel
    {
        [Display(Name = "User Name")]        
        [Remote("Username", "User", ErrorMessage = "User Name already exists")]
        [Required(ErrorMessage = "User Name is required")]
        public string vcr_UserName { get; set; }
    
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be a valid Email Address")]
        [Remote("EmailAddress", "User", ErrorMessage = "Email Address already exists")]
        [Required(ErrorMessage = "Email is required")]
        [Display(Name = "Email Address")]
        public string vcr_EmailAddress { get; set; }
    
        [Display(Name = "Password")]
        [RegularExpression(@"^.{3,16}$", ErrorMessage = "The Password length must be between 3 and 16 characters ")]
        [Required(ErrorMessage = "Password is required")]
        public string vcr_Password { get; set; }
    
        [Display(Name = "Compare Password")]
        [Compare("vcr_Password", ErrorMessage = "Passwords do not match")]
        public string ComparePassword { get; set; }
    
    
        [Required(ErrorMessage = "Country is required")]
        [Display(Name = "Country")]
        public string vcr_Country { get; set; }
    
        [Required(ErrorMessage = "Website is required")]
        [Display(Name = "WebSite")]        
        public string vcr_Website { get; set; }
    
        [Display(Name = "Expertise")]
        [Required(ErrorMessage = "Expertise is required")]
        public string vcr_Expertise { get; set; }
    
        public int int_GroupId { get; set; }
        public Boolean bit_Active { get; set; }
    }
    

    我的控制器

      public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(User Users)
        {
            try
            {
    
                UserRepository UserRep = new UserRepository();
                if (ModelState.IsValid)
                {
                    Users.int_GroupId = 2;
                    Users.dtm_CreatedDate = DateTime.Now;
                    Users.bit_Active = true;
                    UserRep.Add(Users);
                    UserRep.Save();
                }
                return View();
            }
            catch { return View(); }
        }
    

    查看

    @model MvcApplication1.Models.User
       {
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
    

    }

    索引

    @using (Html.BeginForm()) {
    
    
    
      @Html.ValidationSummary(true)
    <fieldset>
        <legend>Users</legend>
    
        <div class="editor-label">
            @Html.LabelFor(model => model.vcr_UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.vcr_UserName)
            @Html.ValidationMessageFor(model => model.vcr_UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.vcr_Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(model => model.vcr_Password)
            @Html.ValidationMessageFor(model => model.vcr_Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ComparePassword)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(model => model.ComparePassword)
            @Html.ValidationMessageFor(model => model.ComparePassword)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.vcr_EmailAddress)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.vcr_EmailAddress)
            @Html.ValidationMessageFor(model => model.vcr_EmailAddress)
        </div>
    
        <p>
            <input type="submit" value="Create" />
        </p>
    
        </fieldset>
    

    }

    好的,当我点击创建我收到上述错误

1 个答案:

答案 0 :(得分:3)

“外场”是什么意思?

已经想到的一件事是:

不应将视图行为的数据注释放在模型类上。 您应该将面向数据库的模型与面向视图的视图模型分开。 有很多关于此的文章。

如果你创建了一个viewmodel,你可以在那里添加第6个字段,添加数据注释,让验证做它的事情等等。

您可以使用AutoMapper或ValueInjecter等工具执行从模型到视图模型的映射,反之亦然,这样您就不必编写太多代码。

如果您有任何其他问题,请随时提出......