这是我的控制器代码:
[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);
}
}
}
我的模型如下:
[System.ComponentModel.DisplayName("buildingName")]
[Remote("CheckBuildingName", "ConfigLocationController",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
我在这方面遇到错误。无法找到控制器路径或未实现IController。那是什么意思。我错过了什么吗?或者我的代码完全错了。 ?请帮忙
答案 0 :(得分:0)
错误的原因是您的RemoteAttribute
正在调用CheckBuildingName
的{{1}}方法。假设您的控制器实际上名为ConfigLocationControllerController
,那么您的属性需要
ConfigLocationController
但是,您的方法也包含错误。您初始化模型的新实例,然后在查询中使用其[Display(Name = "Building Name")] // use this attribute, not DisplayName
[Remote("CheckBuildingName", "ConfigLocation",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
属性的值(将为buildingName
),因此它将始终返回null
。另外,您应该为ajax调用提交的值添加参数,而不是使用null
。你的方法可以简单地
Request.Form
如果没有匹配将返回[HttpPost]
public JsonResult CheckBuildingName(string buildingName)
{
bool exists = context.Buildings.Any(x => x.buildingName == buildingName && x.buildingActive);
return Json(!exists, JsonRequestBehavior.AllowGet);
}
,如果存在则返回true
,在这种情况下,您在属性中定义的消息将显示在视图中,假设您已包含{{ 1}}