MVC Clientside验证回发发生

时间:2016-01-12 06:08:57

标签: jquery asp.net-mvc validation model-view-controller autopostback

我是MVC的新手,我在项目中遇到了非常愚蠢的问题,我读到该模型使用Jquery在MVC中管理客户端验证,所以我已经在我的Bundle中添加了一些必需的jquery。 Config File.Even虽然在验证时页面得到Post backs,即使验证被触发,因为我读它不应该回发如果发生了违规验证,请帮我解决这个问题。

以下是Bundle.Config文件的片段。请检查我错过了什么?

 using System.Web;
  using System.Web.Optimization;

namespace DesignationDemo
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        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"));

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

            bundles.Add(new ScriptBundle("~/bundles/bootstrapjs").Include(
                        "~/Scripts/bootstrap.min.js"));

            bundles.Add(new StyleBundle("~/Content/bootstrapcss").Include(
                        "~/Content/bootstrap.min.css",
                        "~/Content/bootstrap-responsive.min.css"));


            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

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


bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
              "~/Content/themes/base/jquery.ui.core.css",
              "~/Content/themes/base/jquery.ui.resizable.css",
              "~/Content/themes/base/jquery.ui.selectable.css",
              "~/Content/themes/base/jquery.ui.accordion.css",
              "~/Content/themes/base/jquery.ui.autocomplete.css",
              "~/Content/themes/base/jquery.ui.button.css",
              "~/Content/themes/base/jquery.ui.dialog.css",
              "~/Content/themes/base/jquery.ui.slider.css",
              "~/Content/themes/base/jquery.ui.tabs.css",
              "~/Content/themes/base/jquery.ui.datepicker.css",
              "~/Content/themes/base/jquery.ui.progressbar.css",
              "~/Content/themes/base/jquery.ui.theme.css"));



        }
    }
}

查看“创建”操作

@using (Html.BeginForm("Create", "DesignationInfo", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{  
    @Html.ValidationSummary(true)  

    <table>

        <tr> <td>
           <b>Designation Name  </b>
        </td>
        <td>
            @Html.EditorFor(model => model.Name)  
            @Html.ValidationMessageFor(model => model.Name)  
            </td>
        </tr>

        <tr> <td> 
          <b>  Active </b>
        </td>
       <td>
            @Html.CheckBoxFor(model => model.Active, new { @checked = "checked" })


        </td>
            </tr>
    <tr><td>
           </td>                             
             <td></tr>
         <tr> <td> </td><td>

                  <input type="submit" value="Save" class="btn btn-success" />  &nbsp;&nbsp;&nbsp;
             <input type="button" value="Cancel" onclick="Cancel()" class="btn btn-danger" />  &nbsp;&nbsp;&nbsp;
                <input type="button" value="Back" onclick="Close()" class="btn btn-warning" /></td>


        </tr>
    </table>
}  



 <script src="~/Scripts/bootstrap.min.js"></script>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />


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


<script type="text/javascript">  

    function Close() {
        window.location.href = '/DesignationInfo/';

    }
    function Cancel() {
        $('#Name').val("")
        $('#Active').prop('checked', false);
    }
    </script>

模型如下:

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

namespace DesignationDemo.Models
{

    public class Designation
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Can not be blank Name")]
        public string Name { get; set; }
        private bool myVal = true;
        [DefaultValue(true)]        
        public bool Active
        {
            get
            {
                return myVal;
            }
            set
            {
                myVal = value;
            }
        }

    }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您也可以尝试在视图顶部添加,它可能会解决问题....

@model DesignationDemo.Models.Designation

只添加bundle.config是不够的......

您还需要在视图中添加以下代码

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")

还放了一些Model&amp;的代码。查看,我可以查看更多...

忘记提及......

最重要的代码

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