1

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

5
  • You could try public JsonResult CheckEmailExist([Bind(Prefix="ms_person"]string email) {.. Commented Aug 26, 2014 at 4:18
  • try it, but doesn't work :O, searching another solution right now.. Commented Aug 26, 2014 at 4:27
  • Just tested, it should be public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email"]string email) {.. Commented Aug 26, 2014 at 5:07
  • yup, worked perfectly, i think you missing ")" just before "]", itu should be JsonResult CheckEmailExist([Bind(Prefix="ms_person.email")]string email) Thx :D Commented Aug 26, 2014 at 5:17
  • Yes of course (clumsy fingers). I'll post as answer. Commented Aug 26, 2014 at 5:18

2 Answers 2

1

Change you action method to include the Bind Prefix attribute/property

public JsonResult CheckEmailExist([Bind(Prefix="ms_person.email")]string email)
{
  ...
Sign up to request clarification or add additional context in comments.

Comments

0

Simply change it to

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

2 Comments

That does not change the name. Html helpers overwrite any attempt to set the name attribute`
still not working, just asking if i change the name and id using the new{}, does that mean the entity framework will not recognize the id and name, and email will not be inserted to database?? just curious.... haven't try it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.