使用远程validationasp.net MVC

我想尝试使用我在此链接上发现的远程validation: http : //www.youtube.com/watch?v = L8VtDRj8L4

我已经按照说明操作了但问题是,当我尝试从引用的表中添加数据时,validation不起作用

型号类:

public partial class ms_student { public int ID { get; set; } public string student_code{ get; set; } public virtual ms_person ms_person { get; set; } } public partial class ms_person { public string name{ get; set; } public string email { get; set; } public virtual ms_student ms_student { get; set; } } 

元数据:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace Test.Models { [MetadataType(typeof(personMD))] public partial class ms_person { } public class personMD { [Required(ErrorMessage = "Email is required")] [EmailAddress(ErrorMessage = "Invalid Email Address")] [Remote("CheckEmailExist", "Administrator", ErrorMessage = "Email Already Exist")] public object email { get; set; } } } 

控制器:

 public JsonResult CheckEmailExist(string email) // the error i think from email paramater, cause the video said to make the paramater exactly the same name... { return Json(!db.ms_person.Any(m => m.email == email), JsonRequestBehavior.AllowGet); } 

浏览次数:

 @model Test.Models.ms_student @using (Html.BeginForm("CreateStudent", "Administrator", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.TextBoxFor(model => model.student_code) //this one work and already tested @Html.ValidationMessageFor(model => model.student_code) @Html.TextBoxFor(model => model.ms_person.email) //if you inspect element on browser the NAME are ms_person.email and ID are ms_person_email @Html.ValidationMessageFor(model => model.ms_person.email) } 

我试图将JsonResult控制器参数更改为(字符串ms_person.email),但是有错误表示无法找到命名空间电子邮件..也尝试使用(字符串ms_person_email),也无法工作

我还使用student_code进行了测试,student_code字段正常工作,因为student_code属性属于同一模型(ms_student),而不是电子邮件(对ms_person的引用)

所有元数据validation工作,就像两个模型上所需的一样,所以我猜测错误是在JsonResult参数上

非常感谢你

更改操作方法以包括“ Bind Prefix属性/属性

 public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email")]string email) { ... 

只需将其更改为

 @Html.TextBoxFor(model => model.ms_person.email, new{@id="email", @name="email"})