Search code examples
asp.net-mvc-3remote-validation

Remote validation not working for guid


[Remote("DropDownSelected", "Patient")]
public Guid SexIdentifier { get; set; }



public ActionResult DropDownSelected(object value)
{
    var x = ((Guid)value).ToString();
    string xsd = value.ToString();
    var abc = ControllerContext.Controller;

    if (value == null)
    {
        //value = string.Empty;
    }

    if (value.ToString() == Convert.ToString(Guid.Empty) || value.ToString() == string.Empty)
    {
        return Json(String.Format("0 does not exist."), JsonRequestBehavior.AllowGet);
    }

    return Json(true, JsonRequestBehavior.AllowGet);
}

Solution

  • If your property is called SexIdentifier then your action must use the same name for its argument:

    public ActionResult DropDownSelected(Guid sexIdentifier) 
    {
        ...
    }
    

    Also if you have a default value of the dropdown you could use a nullable Guid:

    public ActionResult DropDownSelected(Guid? sexIdentifier) 
    {
        ...
    }