ASP.NET MVC 3远程验证 [英] ASP.NET MVC 3 Remote Validation
问题描述
我有我试图从一个区域内使用该模型的属性以下属性来使用我的项目的路线中验证控制器...
I have a validation controller within the route of my project that I'm trying to use from within an area using the following attribute on the model's property...
[Remote("IsValidUserName", "Validation", "", ErrorMessage = "Invalid username")]
但是,当这个被渲染,验证是针对控制器的相同面积的页中的IsValidUserName操作确认,而不是根部区域内...
But when this is rendered, the validation is against the "IsValidUserName" action of controller "Validation" within the same area as the page, and not within the root area...
数据-VAL-远程URL =/成员/确认/ IsValidUserName
data-val-remote-url="/Members/Validation/IsValidUserName"
任何帮助将是AP preciated。
Any help would be appreciated.
感谢。
推荐答案
不幸的是这个属性是如何实现的。下面是该属性的构造函数的摘录:
Unfortunately that's how this attribute is implemented. Here's an excerpt from the constructor of this attribute:
public RemoteAttribute(string action, string controller, string areaName) : this()
{
if (string.IsNullOrWhiteSpace(action))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "action");
}
if (string.IsNullOrWhiteSpace(controller))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "controller");
}
this.RouteData["controller"] = controller;
this.RouteData["action"] = action;
if (!string.IsNullOrWhiteSpace(areaName))
{
this.RouteData["area"] = areaName;
}
}
注意对 AREANAME
的 IsNullOrWhiteSpace
测试那个的杀一切都结束了吗?
Notice the IsNullOrWhiteSpace
test against the areaName
at the end that's killing everything?
您可以通过编写自定义的远程属性修正:
You could fix it by writing a custom remote attribute:
public class MyRemoteAttribute : RemoteAttribute
{
public MyRemoteAttribute(string action, string controller, string area)
: base(action, controller, area)
{
this.RouteData["area"] = area;
}
}
和则:
[MyRemote("IsValidUserName", "Validation", "", ErrorMessage = "Invalid username")]
public string Username { get; set; }
现在正确的数据-VAL-远程URL =/验证/ IsValidUserName
将产生。
这篇关于ASP.NET MVC 3远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!