基于MVC中的远程验证的错误 [英] Error based on Remote Validation in mvc
问题描述
这是我的控制器代码:
[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.那是什么意思.我想念什么吗?还是我的代码完全错误. ?请帮助
发生错误的原因是您的RemoteAttribute
正在调用ConfigLocationControllerController
的CheckBuildingName
方法.假设您的控制器实际上被命名为ConfigLocationController
,则您的属性必须为
[Display(Name = "Building Name")] // use this attribute, not DisplayName
[Remote("CheckBuildingName", "ConfigLocation",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
但是,您的方法也包含错误.您初始化模型的新实例,然后在查询中使用其buildingName
属性的值(将为null
),因此它将始终返回null
.另外,您应该为ajax调用提交的值添加一个参数,而不是使用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
;如果没有匹配项,则返回false
,在这种情况下,假设您已包含@Html.ValidationMessageFor(m => m.buildingName)
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 the CheckBuildingName
method of ConfigLocationControllerController
. Assuming that you controller is actually named ConfigLocationController
, then you attributes need to be
[Display(Name = "Building Name")] // use this attribute, not DisplayName
[Remote("CheckBuildingName", "ConfigLocation",ErrorMessage = "Building already exists!")]
public string buildingName { get; set; }
However your method also contains errors. You initialize a new instance of a model and then use the value of its buildingName
property (which will be null
) in your query so it will always return null
. In additional, you should add a parameter for the value your ajax call is submitting rather than using Request.Form
. You method can be simply
[HttpPost]
public JsonResult CheckBuildingName(string buildingName)
{
bool exists = context.Buildings.Any(x => x.buildingName == buildingName && x.buildingActive);
return Json(!exists, JsonRequestBehavior.AllowGet);
}
which will return true
if there is no match, or false
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)
这篇关于基于MVC中的远程验证的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!