Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 客户端验证不适用于自定义属性_C#_Asp.net Mvc 2_Asp.net Mvc 2 Validation - Fatal编程技术网

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分