Here is what my code look like rendered as HTML
<input data-val="true" data-val-remote="User name already exists. Please enter a different user name." data-val-remote-additionalfields="*.Name" data-val-remote-url="/OrchardLocal/Genealex.CRM.Core/Home/doesNameExist" data-val-required="Enter Name" id="typeName" name="TypeType.Name" type="text" value="">
Here is the property in my model
[Remote("doesNameExist", "Home", ErrorMessage = "User name already exists. Please enter a different user name.")]
public string Name { get; set; }
The action in my controller:
[HttpPost]
public JsonResult doesNameExist(string Name)
{
var user = _contactBll.DoesTypeExist(Name);
if (user)
{
return Json("Username is already exist", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
And in my View I have :
@Html.TextBoxFor(model => model.TypeType.Name, new { id = "typeName" })
I have also included scripts like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>
Yet, when I leave the focus from the textbox, after I have written something in the input field the validation is not fired.
What am I doing wrong?
My mistake was the following :
Instead of [HttpPost], I needed the [HttpGet] over my controller. Switched it up and works just fine.