ASP.NET MVC:万无一失的验证不起作用

时间:2016-10-06 08:54:01

标签: c# asp.net-mvc foolproof-validation

在这个thread中,我得到了使用FoolProof解决问题的建议。这就是我现在想做的事情。但是,我找不到Foolproof在我的项目中不起作用的原因。所以我会给你一个视图以及我如何在那里包含Foolproof,模型以及我如何添加Foolproof作为一个包,所以也许你有人看到我的错误。

查看:

 @section Scripts {
         @Scripts.Render("~/bundles/jqueryval")
         @Scripts.Render("~/bundles/mvcfoolproof")
            }

BundleConfig:

public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/datatables").Include(
                        "~/Scripts/DataTables/jquery.dataTables.js"));

            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css",
                      "~/Content/jquery-ui.css",
                      "~/Content/dataTables.css"));

            bundles.Add(new ScriptBundle("~/bundles/mvcfoolproof").Include(
                    "~/Scripts/MicrosoftAjax.js",
                    "~/Scripts/MicrosoftMvcAjax.js",
                    "~/Scripts/MicrosoftMvcValidation.js",
                    "~/Scripts/MvcFoolproofJQueryValidation.min.js",
                    "~/Scripts/MvcFoolproofValidation.min.js",
                    "~/Scripts/mvcfoolproof.unobtrusive.min.js"));

        }

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Foolproof;

public int CustomerID {get; set;}
public bool ValRequired {get; set;}
[RequiredIfTrue("ValRequired")]
public string NameofCustomer { get; set; }

所以,问题是,如果我这样应用应用程序运行没有错误,但此时没有验证跳转,所以我会感谢任何帮助! :)

1 个答案:

答案 0 :(得分:1)

在BundleConfig中,替换您添加的最后一个

bundles.Add(new ScriptBundle("~/bundles/mvcfoolproof").Include(
            "~/Client Scripts/mvcfoolproof.unobtrusive.js",
            "~/Client Scripts/mvcfoolproof.unobtrusive.min.js",
            "~/Client Scripts/MvcFoolproofJQueryValidation.js",
            "~/Client Scripts/MvcFoolproofJQueryValidation.min.js",
            "~/Client Scripts/MvcFoolproofValidation.js",
            "~/Client Scripts/MvcFoolproofValidation.min.js"));

安装Foolproof软件包时,js文件位于Client Scripts文件夹中,而不是Scripts文件夹中。