This is my controller code:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult CheckBuildingName()
{
var isUnique = true;
string _buildingName = Request.Form["buildingName"]; // put your control name here
var connectionstring = ConnectionProvider();
AddBuildingModel model = new AddBuildingModel();
using (var context = new Notifier.AccountDatabase(connectionstring))
{
var objBuilding = (from building in context.Buildings
where building.buildingName == model.buildingName && building.buildingActive == true
select building).FirstOrDefault();
if (objBuilding == null)
{
isUnique = true;
}
else
{
isUnique = false;
}
}
if (isUnique == false)
{
return Json("Building already taken, Pleaes try with different name.", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
and my model is like below:
[System.ComponentModel.DisplayName("buildingName")]
[Remote("CheckBuildingName", "ConfigLocationController",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
I am getting errors on this. The controller path cannot be found out or does not implement IController. What does that mean. Am I missing something ? Or is my code completely wrong. ? Please help
The reason for the error is that your
RemoteAttribute
is calling theCheckBuildingName
method ofConfigLocationControllerController
. Assuming that you controller is actually namedConfigLocationController
, then you attributes need to beHowever your method also contains errors. You initialize a new instance of a model and then use the value of its
buildingName
property (which will benull
) in your query so it will always returnnull
. In additional, you should add a parameter for the value your ajax call is submitting rather than usingRequest.Form
. You method can be simplywhich will return
true
if there is no match, orfalse
if there is, in which case the message you have defined in the attribute will be displayed in the view assuming you have included@Html.ValidationMessageFor(m => m.buildingName)