0

I'm having trouble using database first approach model validations, i read famous ASP.NET MVC 2: Model Validation by Scott Gu, but the problem is it did not work in my mvc project, I'm having my Edmx file in Project.Model class library ,and my validation class in Project.Model.Membership namespace, I don't really get the concept of a problem here. here is the code:

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; }
}
aRasH
  • 144
  • 1
  • 3
  • 12

1 Answers1

0

ok, so firstly, take a look on this: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

Secondly, I would recommend to use ViewModels to validate objects. Like in code below (in shortcut):

MemberViewModel.cs

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

    [Required]
    public string mLName { get; set; }
}

And then, send your ViewModel to Edit/Add view:

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" />
}

Controler and Add 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);
    }

With this approach, you have clear Entity model (POCO classes like Member) and Domian models (ViewModels like MemberViewModel) with custom validation.

whoah
  • 4,363
  • 10
  • 51
  • 81
  • view models are good ,but if I wanted to do that it was better to go for code first approach, now that I'm using database first, it's kinda extra work and code using view model instead of using generated model classes,I think it relates to my namespaces, but I don't really know how to fix it! – aRasH Dec 22 '13 at 10:14
  • no, viewmodels are not only for code first approach. This is pattern used also in code first and database first. – whoah Dec 22 '13 at 13:01
  • Thanks, I think It's better that pulling my hair over this meta data thing – aRasH Dec 25 '13 at 06:39