Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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_Data Annotations - Fatal编程技术网

C# 数据验证无效

C# 数据验证无效,c#,asp.net-mvc,data-annotations,C#,Asp.net Mvc,Data Annotations,我创建了一个非常简单的代码来测试CustomValidations,但它不起作用: 验证 using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace UniversidadeCorporativa.Util { public class CustomDDD : ValidationAttribute { private Regex _regex =

我创建了一个非常简单的代码来测试CustomValidations,但它不起作用:

验证

using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace UniversidadeCorporativa.Util
{
    public class CustomDDD : ValidationAttribute
    {
    private Regex _regex = new Regex(@"^[1-9]{2}$");

    public override bool IsValid(object value)
    {

        if (_regex.IsMatch(value.ToString()))
        {
            return true;
        }

        return false;
    }
}
视图模型

using UniversidadeCorporativa.Util;
using System.ComponentModel.DataAnnotations;
namespace UniversidadeCorporativa.ViewModels
{
    public class TesteViewModel
    {
        public TesteViewModel()
        { }

        [Required]
        [CustomDDD]
        [Display(Name = "DDD")]
        public int DDDCel { get; set; }

        [Required]
        [Display(Name = "Celular")]
        public int Celular { get; set; }
    }
}
控制器

using UniversidadeCorporativa.ViewModels;
public ActionResult Teste()
{
    return View();
}

[HttpPost]
public ActionResult Teste(TesteViewModel model)
{
    try
    {
        return RedirectToAction("Teste");
    }
    catch
    {
        return View();
    }
}
看法

@model UniversidadeCorporativa.ViewModels.TesteViewModel
@{
ViewBag.Title=“Teste”;
}
睾丸
@使用(Html.BeginForm(“Teste”,“Universidade”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
@*@Html.ValidationSummary(“,new{@class=“text danger”})*@
@LabelFor(m=>m.Celular,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.DDDCel,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.DDDCel)
@TextBoxFor(m=>m.Celular,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Celular)
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}

提交输入时没有任何验证,我不知道为什么。有人能帮忙吗?

当您添加自定义验证时,您必须通过检查Teste操作中的
ModelState.IsValid
来测试it服务器端:

[HttpPost]
public ActionResult Teste(TesteViewModel model)
{
    try
    {
        if(ModelState.IsValid)
        {
            // your model is valid!
        }

        return RedirectToAction("Teste");
    }
    catch
    {
        return View();
    }
}

目前,CustomValidation属性不会自动连接到jQuery的非干扰性验证库中。因此,在创建自定义jQuery验证规则之前,它不会在客户端工作。请参阅以获取参考。

当您添加自定义验证时,您必须通过检查Teste操作中的
ModelState.IsValid
来测试it服务器端:

[HttpPost]
public ActionResult Teste(TesteViewModel model)
{
    try
    {
        if(ModelState.IsValid)
        {
            // your model is valid!
        }

        return RedirectToAction("Teste");
    }
    catch
    {
        return View();
    }
}

目前,CustomValidation属性不会自动连接到jQuery的非干扰性验证库中。因此,在创建自定义jQuery验证规则之前,它不会在客户端工作。请参阅以供参考。

您希望在何处执行验证?我没有看到任何代码进行任何验证。(我没有看到您在控制器中检查
ModelState.IsValid
,也没有看到任何进行验证的javascript)。MVC已经有一个
RegularExpressionAttribute
(它提供客户端和服务器端验证)。你为什么要重新发明轮子?(您可以始终扩展该属性,以便将正则表达式内置到其中,并且您可以使用
[CustomDDD]
而不是
[RegularExpression(@“^[1-9]{2}$”)
在何处执行验证?我没有看到任何执行任何验证的代码。(我没有看到您在控制器中检查
ModelState.IsValid
,也没有看到任何进行验证的javascript)。MVC已经有一个
RegularExpressionAttribute
(它提供客户端和服务器端验证)。您为什么要尝试重新发明轮子?(您可以始终扩展该属性,以便将正则表达式内置到该属性中,您可以只使用
[CustomDDD]
而不是
[RegularExpression(@“^[1-9]{2}$”)