i like to try using remote validation that i spot on this link: http://www.youtube.com/watch?v=Ll8VtDRj8L4
i've followed the instruction and it worked but the problem is, when i try to add data from referenced table, the validation wont worked
Model Class:
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; }
}
Metadata:
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; }
}
}
Controller:
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);
}
Views:
@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)
}
i've tried to change the JsonResult Controller paramater into (string ms_person.email), but there error said namespace email could not be found.. also tried to use (string ms_person_email), won't work either
i've also tested using student_code, the student_code field work properly because the student_code attribute are in the same Model(ms_student), not like email(reference to ms_person)
All metadata validation work, like required on both Model, so i'm guessing the error is on the JsonResult parameter
Thank you very much
public JsonResult CheckEmailExist([Bind(Prefix="ms_person"]string email) {..public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email"]string email) {..