【问题标题】:database first data annotation Issue asp.net mvc 4数据库第一个数据注释问题asp.net mvc 4
【发布时间】:2013-12-22 09:17:44
【问题描述】:

我在使用数据库优先方法模型验证时遇到问题,我阅读了 Scott Gu 的著名 ASP.NET MVC 2: Model Validation,但问题是它在我的 mvc 项目中不起作用,我在 Project.Model 类中有我的 Edmx 文件库,以及我在 Project.Model.Membership 命名空间中的验证类,我在这里并没有真正理解问题的概念。 这是代码:

namespace Project.Model
//part of generated code by EF database first
public partial class Member
{
    public Member()
    {
        this.SideDuties = new HashSet<SideDuty>();
        this.Member_In_Role = new HashSet<Member_In_Role>();
        this.Messages = new HashSet<Message>();
        this.Messages1 = new HashSet<Message>();
    }

    public System.Guid mId { get; set; }
    public byte MemberTypeNo { get; set; }
    public string mName { get; set; }
    public string mLName { get; set; }
    public string mUserName { get; set; }
    public string mPass { get; set; }
    public Nullable<byte> MarriageStatusNo { get; set; }
    public Nullable<byte> GenderNo { get; set; }
    public Nullable<int> mPhone { get; set; }
    public Nullable<long> mMobile { get; set; }
    public Nullable<int> mEmrgPhone { get; set; }
    public Nullable<long> mEmrgMobile { get; set; }
    public string mEmail { get; set; }
    public string mProfilePicExt { get; set; }
    public bool mIsOperator { get; set; }
    public bool mIsAdmin { get; set; }

    public virtual ...
}



namespace Project.Model.membership
//my class handling data annotations, not work!
[MetadataType(typeof(Member_Validation))]
public partial class Member
{

}

//buddy class
[Bind(Exclude = "mId")]
public sealed class Member_Validation
{
    //public System.Guid mId { get; set; }
    public byte MemberTypeNo { get; set; }
    [Required(ErrorMessage = "blah blah")]
    public string mName { get; set; }
    [Required]
    public string mLName { get; set; }
    public string mUserName { get; set; }
    public string mPass { get; set; }
    public Nullable<byte> MarriageStatusNo { get; set; }
    public Nullable<byte> GenderNo { get; set; }
    public Nullable<int> mPhone { get; set; }
    public Nullable<long> mMobile { get; set; }
    public Nullable<int> mEmrgPhone { get; set; }
    public Nullable<long> mEmrgMobile { get; set; }
    public string mEmail { get; set; }
    public string mProfilePicExt { get; set; }
    public bool mIsOperator { get; set; }
    public bool mIsAdmin { get; set; }
}

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-4 data-annotations model-validation


    【解决方案1】:

    好的,所以首先,看看这个: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

    其次,我建议使用 ViewModels 来验证对象。就像下面的代码(快捷方式):

    MemberViewModel.cs

    public class MemberViewModel
    {
        [Required]
        [StringLength(10)]
        public string mName { get; set; }
    
        [Required]
        public string mLName { get; set; }
    }
    

    然后,将您的 ViewModel 发送到编辑/添加视图:

    Add.cshtml

    @Model MemberViewModel //namespace etc.
    
    
    @using (Html.BeginForm("Add"))
    {
    
    @Html.ValidationSummary()
    @ViewBag.Status
        @Html.LabelFor(m => m.mName)
        @Html.TextBoxFor(m => m.mName)
        @Html.ValidationMessageFor(m => m.mName)
    
        @Html.LabelFor(m => m.mLName)
        @Html.TextBoxFor(m => m.mLName)
        @Html.ValidationMessageFor(m => m.mLName)
    
        <input type="submit" />
    }
    

    控制器和添加 ActionResult

        [HttpPost]
        public ActionResult Add(MemberViewModel model)
        {
    
            if (ModelState.IsValid)
            {
                Member memberToAdd = new Member{ };
    
                memberToAdd.mLName = model.mLName;
                memberToAdd.mName = model.mName;
                (..)
    
                //some operation, perhaps on database, with memberToAdd
    
                return RedirectToAction("xyz");
            }
            else
                return View(model);
        }
    

    使用这种方法,您将拥有清晰的实体模型(POCO 类,如 Member)和 Domian 模型(ViewModels,如 MemberViewModel),并带有自定义验证。

    【讨论】:

    • 视图模型很好,但如果我想这样做,最好采用代码优先的方法,现在我首先使用数据库,使用视图模型而不是使用视图模型和代码有点额外的工作使用生成的模型类,我认为它与我的命名空间有关,但我真的不知道如何修复它!
    • 不,视图模型不仅适用于代码优先方法。这也是代码优先和数据库优先的模式。
    • 谢谢,我认为最好把头发拉到这个元数据上
    猜你喜欢
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多