I have made a remote validation in my project, to avoid duplicate entries in the DB. My metadata is like this:
[Remote("CheckUserName", "Home", ErrorMessage = "This is a duplicate program ")]
public string UserName { get; set; }
And inside my HomeController I have the function like this
public JsonResult CheckUserName(string UserName)
{
if (!RUser.DupUserName(UserName))
{
return Json(true, JsonRequestBehavior.AllowGet);
}
else
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
In my create View
<td>@Html.EditorFor(model => model.UserName)</td>
<td>@Html.ValidationMessageFor(model => model.UserName)</td>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
in the repository
public bool DupUserName(string UserName)
{
int q = (from a in db.Tbl_User
where a.UserName.Equals(UserName)
select a).Count();
return Convert.ToBoolean(q);
}
Everything looks okay to me, but this validation does not work. Can any one point out what I am doing wrong here? I also tried using the overload of RemoteAttribute but it doen't work.