验证根本不起作用

时间:2018-06-05 20:22:59

标签: c# asp.net-mvc validation

从GitHub下载我的项目并更新软件包后(我认为这就是原因),我遇到了验证模型的问题。我不是一个完整的新手,但我所知道的每一种方法都无法解决。

我最近在这里问了一个关于ajax表单验证的问题,但事实证明整个验证系统不起作用。

我有一个带有验证属性的实体,分为两类:

namespace JustBlog.Core.Objects
{

    [MetadataType(typeof(TagMetaData))]
    public partial class Tag
    {
        public int Id
        { get; set; }

        public string Name
        { get; set; }

        public string UrlSlug
        { get; set; }

        public string Description
        { get; set; }

        public ICollection<Post> Posts
        { get; set; }
    }
}

它的第二部分有属性:

namespace JustBlog.Core.Objects
{
   [DisplayName("tags")]
    public partial class TagMetaData
    {
        [ScaffoldColumn(false)]
        public int Id
        { get; set; }

        [Required(ErrorMessage = "req")]
        [MinLength(3, ErrorMessage = "min")]
        [MaxLength(50, ErrorMessage = "max")]
        [DataType(DataType.Text)]
        public string Name
        { get; set; }

        [Required(ErrorMessage = "req")]
        [Display(Name = "Query параметр")]
        [MinLength(3, ErrorMessage = "omitting")]
        [MaxLength(50, ErrorMessage = "")]
        [RegularExpression(@"^[a-zA-z-_0-9]{3,}$")]
        [DataType(DataType.Text)]
        public string UrlSlug
        { get; set; }

        [Required]
        [MinLength(3, ErrorMessage = "3")]
        [MaxLength(200, ErrorMessage = "200")]
        [DataType(DataType.MultilineText)]
        public string Description
        { get; set; }
    }
}

正如标题所说,即使我输入了错误的数据,来自ModelState的道具IsValid也总是如此。

以下是没有任何ajax请求的编辑操作的测试示例,它也失败了。

        [HttpGet]
        public ActionResult TestingMethodTag()
        {
            return View(new Tag { Id = 3, Description = "Some text", Name = "Name of tag", Posts = null, UrlSlug = "url-slug"});
        }

        [HttpPost]
        public ActionResult TestingMethodTag(Tag tag)
        {
            if (ModelState.IsValid)
            {
               return View(new Tag { Id = 3, Description = "another info", Name = "Name of tag", Posts = null, UrlSlug = "slugg" });
            }
            else
            //never runs
            return View(tag);
        }

以下是此操作方法的视图:( jQuery 加载布局,即使在web.config中也启用了客户端验证)

@model JustBlog.Core.Objects.Tag

@{
    ViewBag.Title = "TestingMethodTag";
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}

@using (Html.BeginForm())
{
    <div class="form-horizontal">

        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UrlSlug, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UrlSlug, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UrlSlug, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}


@section scripts {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" ></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" ></script>
}

我没有应用于实体的验证消息:

<span class="field-validation-valid text-danger" data-valmsg-for="Description" data-valmsg-replace="true"></span>

似乎ValidationMessageFor()只是忽略所有规则/无法找到它们;

有什么想法吗?

0 个答案:

没有答案