【发布时间】:2016-03-20 08:56:48
【问题描述】:
我有使用引导模式的 CRUD 控制器。我有实体 Town,我想实现 TownName 属性是唯一的。所以我使用 Remote 属性:
public class Town
{
...
[Required]
[Remote("IsTownValid", "Town"]
public string TownName { get; set; }
...
}
在控制器中我有:
public JsonResult IsTownValid(string townName)
{
return IsTownExist(townName) ? Json(false, JsonRequestBehavior.AllowGet) : Json(true, JsonRequestBehavior.AllowGet);
}
private bool IsTownExist(string townName)
{
// repository-unitOfWork that get town with specified town name
var town = repository.TownRepository.Get(filter: t => t.TownName == townName).SingleOrDefault();
if (String.IsNullOrEmpty(townName))
return true;
else if (town != null)
return true;
else
return false;
}
我在 _Layout 视图中注册了 Microsoft jQuery Unobtrusive Validation 库。
问题是当我启动创建城镇的模式时,模式忽略远程属性。因此,出于测试目的,我在该控制器标准中创建创建页面和远程验证工作完美。所以,我的问题是为什么不在模态中工作?
建议,请...在此先感谢...
【问题讨论】:
-
可能是因为模态正在动态加载而您没有重新解析验证器?旁注:它可以只是
Json(!IsTownExist(townName), JsonRequestBehavior.AllowGet); -
您可能还想再次查看
IsTownExist()方法中的逻辑。if (String.IsNullOrEmpty(townName)) { return true; }应该在您调用数据库之前。
标签: c# asp.net asp.net-mvc twitter-bootstrap