我在使用数据库第一种方法模型验证时遇到了麻烦,我读了Scott Gu的着名ASP.NET MVC 2: Model Validation,但问题是它在我的mvc项目中没有用,我在Project中有我的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; }
}
答案 0 :(得分:0)
好的,首先,请看一下: 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" />
}
Controler和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);
}
使用这种方法,您可以使用自定义验证清晰的实体模型(POCO
类Member
)和Domian模型(ViewModels
,如MemberViewModel)。