Ajax不显眼的验证和MVC4

时间:2014-10-29 14:17:25

标签: jquery ajax asp.net-mvc-4 validation scripting

我正在尝试让我的AJAX在 PARTIAL VIEW 中工作。我已将它包含在我的布局页面的顶部:

        <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - COMIS</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />

        <link href="~/Content/themes/base/jquery.ui.core.css" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")" rel="stylesheet" 
        type="text/css" />

        <script src="~/Scripts/jquery-1.11.1.js" type="text/javascript"></script> 

        <script src="../../Scripts/jquery-ui-1.11.1.js" type="text/javascript"></script>
        <script src="~/Scripts/jquery-migrate-1.2.1.js" type="text/javascript"></script>
        <script src="../../Scripts/DatePickerReady.js" type="text/javascript"></script>

        <script src="~/Scripts/jquery-ui-1.11.1.validate.min.js" type="text/javascript"></script>

        <script src="~/Scripts/jquery.validate.js" type="text/javascript"></script>
         <script src="~/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

        <script src="~/Scripts/jquery-1.11.1.min.js" type="text/javascript"></script> 
        <script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>   
        <script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">       
        </script>

        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css") 
        @Styles.Render("~/Content/themes/base/css") 
        @Scripts.Render("~/bundles/modernizr")
        @Scripts.Render("~/bundles/jquery") /
        @Scripts.Render("~/bundles/jqueryui")
        @Scripts.Render("~/bundles/jqueryval")


        @Scripts.Render("~/Scripts/buttons.js") 
        @Scripts.Render("~/Scripts/navigation.js")
    </head>

然而它不起作用。它抛出错误:“JavaScript运行时错误:无法获取未定义或空引用mvc 4的属性'不引人注意'。我认为它是因为我引用了太多脚本。有人可以请我告诉我我做错了吗?

BUNDLE.config

      public class BundleConfig
        {

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

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

                bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
            "~/Content/themes/base/jquery.ui.core.css",
            "~/Content/themes/base/jquery.ui.datepicker.css",
            "~/Content/themes/base/jquery.ui.theme.css")); 

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


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

                bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css", 
       "~/Content/PagedList.css"));

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


                bundles.IgnoreList.Clear();
   bundles.IgnoreList.Ignore("*.intellisense.js");
                bundles.IgnoreList.Ignore("*-vsdoc.js");
                bundles.IgnoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);


            }
        }

**PARTIAL VIEW** - 

    @model COMIS.Models.X
    @using(Html.BeginForm("Create"))
         { @Html.ValidationSummary(true)


        <fieldset class="innerform">
        <div>
            ....
        </div>
    </fieldset>

    } 


       <script src="~/Scripts/jquery-1.11.1.js" type="text/javascript"></script>
       <script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

2 个答案:

答案 0 :(得分:1)

尝试在布局页面底部按顺序引用脚本。

示例:

   <script src="~/Scripts/jquery-2.1.1.js"></script>
   <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
   <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
   <script src="~/Scripts/jquery.validate.js"></script>
   <script src="~/Scripts/jquery.validate.min.js"></script>
   <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
   <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>

答案 1 :(得分:0)

add all the other scripts to the BundleConfig.cs

<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - COMIS</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css") 
@Styles.Render("~/Content/themes/base/css") 
@Scripts.Render("~/bundles/modernizr")
</head>
<body>

</body>

@Scripts.Render("~/bundles/jquery") /
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/buttons.js") 
@Scripts.Render("~/Scripts/navigation.js")



Edit

// BundleConfig.cs

// for js scripts
bundles.Add(new ScriptBundle("~/bundles/*Bundle Name*").Include(
                        "~/Scripts/file.js")); // location of your js file

// reference js bundle like this
@Scripts.Render("~/bundles/*Bundle Name*")

// css
bundles.Add(new StyleBundle("~/Content/*Bundle Name*").Include(
                        "~/Content//css/file.css")); // location of css file

// reference css bundle like this
@Styles.Render("~/Content/*Bundle Name*")