[RESOLVED]Display or hide validation field message in view page by controller action

I have the value of input name by Jquery .Blur() and can to pass it to below action after lost the focus input in my registration form.

I like just to display or hide validation message after the my condition is done or not like this:

but my ModelState is not happened.

[HttpPost]
public bool ReturnExistedDupplicateNames(string name)
{
var client = new PostDataServiceClient();
var matchingOnes = client.GetSuppliersPatternMatchedWithExistingSuppliers(name, 0);
if (matchingOnes != null && matchingOnes.Count > 0)
{
   ModelState.AddModelError("Name", "Duplicate names exist");
    return true;
}
else
{
    return false; // Json(true, JsonRequestBehavior.AllowGet);
}
}

Hi  hmsarab,

Thanks for your post.

Did you add this in the view?

@Html.ValidationMessageFor(model => model.YourProperty)

Hope this can behelpful.

Best Regards,

Eileen

hmsarab

   ModelState.AddModelError("Name", "Duplicate names exist");
    return true;

Here you need to return the view where you want to show the message

[HttpPost]
 public ActionResult ReturnExistedDupplicateNames(string name)
 {
 var client = new PostDataServiceClient();
 var matchingOnes = client.GetSuppliersPatternMatchedWithExistingSuppliers(name, 0);
 if (matchingOnes != null && matchingOnes.Count > 0)
 {
   ModelState.AddModelError("Name", "Duplicate names exist");
     return view("viewname_where_the_requestcamefrom");
 }
 else
 {
     return  Json(true, JsonRequestBehavior.AllowGet);
 }
 }

Leave a Reply