C# 客户端验证不适用于自定义属性
嗨 我有一个自定义属性C# 客户端验证不适用于自定义属性,c#,asp.net-mvc-2,asp.net-mvc-2-validation,C#,Asp.net Mvc 2,Asp.net Mvc 2 Validation,嗨 我有一个自定义属性 public class NameAttribute : RegularExpressionAttribute { public NameAttribute() : base("abc*") { } } 这可以在服务器端工作,但不能在客户端工作,但是 [RegularExpressionAttribute("abc*",ErrorMessage="asdasd")] public String LastName { get; set; } 两者都适用。
public class NameAttribute : RegularExpressionAttribute
{
public NameAttribute() : base("abc*") { }
}
这可以在服务器端工作,但不能在客户端工作,但是
[RegularExpressionAttribute("abc*",ErrorMessage="asdasd")]
public String LastName { get; set; }
两者都适用。我读了,但没用
我非常感谢你的帮助
谢谢您可能需要在
应用程序启动
中注册与此自定义属性关联的数据注释模型验证提供程序
:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(NameAttribute), typeof(RegularExpressionAttributeAdapter)
);
}
您还可以结帐
这是我用来测试这个的完整例子
型号:
public class NameAttribute : RegularExpressionAttribute
{
public NameAttribute() : base("abc*") { }
}
public class MyViewModel
{
[Name(ErrorMessage = "asdasd")]
public string LastName { get; set; }
}
控制器:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
}
return View(model);
}
}
视图:
x、 姓氏)%%>
x、 姓氏)%%>
x、 姓氏)%%>
加上我前面显示的
应用程序\u Start
注册。您可能需要在应用程序\u Start
中注册与此自定义属性关联的DataAnnotationsModelValidatorProvider
:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(NameAttribute), typeof(RegularExpressionAttributeAdapter)
);
}
您还可以结帐
这是我用来测试这个的完整例子
型号:
public class NameAttribute : RegularExpressionAttribute
{
public NameAttribute() : base("abc*") { }
}
public class MyViewModel
{
[Name(ErrorMessage = "asdasd")]
public string LastName { get; set; }
}
控制器:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
}
return View(model);
}
}
视图:
x、 姓氏)%%>
x、 姓氏)%%>
x、 姓氏)%%>
加上我之前展示的
应用程序启动注册。谢谢你的帖子。我希望我能给你的不仅仅是1分谢谢你的帖子。我希望我能给你不止1分