客户端验证mvc无法正常工作

时间:2014-08-13 05:04:29

标签: jquery asp.net-mvc-4 jquery-validate data-annotations unobtrusive-validation

我正在开发一个mvc4项目。

布局页面:

<html lang="en">
<head>
<meta charset="utf-8" />
    @Scripts.Render("~/bundles/MainScripts")
    @Scripts.Render("~/bundles/JqueryUIScripts")
    @Styles.Render("~/Content/MainCss")
    @Styles.Render("~/Content/JqueryUICss")
 </head>
 <body>

        @RenderBody()

      </body>
  </html>

软件包:

        //Script Bundles
        //Basic Scripts
        bundles.Add(new ScriptBundle("~/bundles/MainScripts").Include(
                    "~/Scripts/jQuery1.9.1.js",
                    //"~/Scripts/jquery-1.10.2.js",//For ui and fileupload plugin
                    "~/Scripts/jquery.validate.min.js",
                    "~/Scripts/jquery.validate.unobtrusive.js",
                    "~/Scripts/knockout-3.0.0.js",
                    "~/Scripts/jquery.ui.core.js",
                    "~/Scripts/jquery.ui.widget.js"
            ));
        //Jquery UI Scripts
        bundles.Add(new ScriptBundle("~/bundles/JqueryUIScripts").Include(
            "~/Scripts/jquery-1.10.2.js",
                    "~/Scripts/jquery.ui.effect-slide.js",
                    "~/Scripts/jquery.ui.datepicker.js",
                    "~/Scripts/jquery.ui.effect-drop.js",
                    "~/Scripts/jquery.ui.position.js",
                    "~/Scripts/jquery.ui.menu.js",
                    "~/Scripts/jquery.ui.autocomplete.js"
          ));
        //File Upload Scripts
        bundles.Add(new ScriptBundle("~/bundles/FileUploadScripts").Include(
                    "~/Scripts/file-upload/jquery.fileupload.js",
                    "~/Scripts/file-upload/jquery.fileupload-ui.js",
                    "~/Scripts/file-upload/jquery.iframe-transport.js",
                    "~/Scripts/file-upload/jquery.form.js",
                    "~/Scripts/file-upload/jquery.uploadfile.js",
                    "~/Scripts/jquery.unobtrusive-ajax.js"
          ));

        //Style Bundle
        //Basic Css
        bundles.Add(new StyleBundle("~/Content/MainCss").Include(
            "~/Content/css/inner.css"
            ));
        //Jquery UI Css
        bundles.Add(new StyleBundle("~/Content/JqueryUICss").Include(
            "~/Content/css/jquery.ui.datepicker.css",
            "~/Content/css/jquery.ui.all.css",
            "~/Content/css/jquery.ui.theme.css",
            "~/Content/css/jquery.ui.core.css",
            "~/Content/css/jquery.ui.autocomplete.css"
            ));
        //File Upload Css
        bundles.Add(new StyleBundle("~/Content/FileUploadCss").Include(
            "~/Content/css/uploadfile.css"
            ));

Root web.config:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

使用System.ComponentModel.DataAnnotations查看模型:

 public class EmployeeVM
{
public Int32 EmployeeId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}

查看通过此&#39; EmployeeVM&#39;:

 @model addsds.Web.ViewModels.EmployeeVM
 @Scripts.Render("~/bundles/FileUploadScripts")
 @Styles.Render("~/Content/FileUploadCss")

@using (Html.BeginForm("Add", "Employee"))
{
   @Html.ValidationSummary(true);
        @Html.Label("First Name", new { @class = "lable" })
        @Html.TextBoxFor(m => m.FirstName, new { @class = "input-box" })
        @Html.Label("Last Name", new { @class = "lable" })
        @Html.TextBoxFor(m => m.LastName, new { @class = "input-box" })
      <input type="submit" name="action:Add" title="" value="Save" class="button">
}

客户端验证无效...单击按钮保存时,它指向控制器方法。我希望在命中控制器之前,FirstName和LastName的文本框在为空时具有边框红色。我错过了什么?

2 个答案:

答案 0 :(得分:0)

只需解决这些问题:

1。)在global.asax文件中注册包。

2.。)不要包含相同jquery文件的不同版本。

3.尝试使用$.noConflict()来解决不同jquery文件之间的冲突。阅读更多http://api.jquery.com/jquery.noconflict/

答案 1 :(得分:0)

您必须为需要验证的文本框添加@ Html.ValidationMessageFor(),如下所示:

@ Html.ValidationMessageFor(m =&gt; m.FirstName)

@ Html.ValidationMessageFor(m =&gt; m.LastName)

并且在post方法中检查Modelstate.isvalid()是否在执行代码之前。