【问题标题】:MVC Clientside Validation postback occures发生 MVC 客户端验证回发
【发布时间】:2016-01-12 06:08:56
【问题描述】:

我是 MVC 新手,在我的项目中遇到了非常愚蠢的问题,我读到该模型使用 Jquery 在 MVC 中管理客户端验证,因此我已经在我的 Bundle 中添加了一些必需的 jquery。配置文件。即使在验证时页面得到回发,即使验证被触发,我读过它不应该在发生验证违规时回发,请帮助我解决这个问题。

下面是Bundle.Config文件的sn-p。请检查我是否遗漏了什么?

 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;
            }
        }

    }
}

提前致谢。

【问题讨论】:

  • 您的 web.config 文件中是否启用了客户端验证?还要使用浏览器工具检查脚本是否实际加载到视图中。
  • 根据您的编辑,您的视图中没有包含 ~/bundles/jqueryval 脚本
  • 感谢@StephenMuecke:根据您的建议,我也包含了该文件,但仍然会发生回发。我认为bundle.config中存在一些问题,因为这种问题也出现在新添加的页面中。
  • 我刚刚测试了您的代码,模型和视图都在我身边...看起来工作正常...我很快会将我的示例上传到 Google Drive 并分享给您...可能是您可以比较我的示例项目和您的项目并找出差异...如果可能的话,您还可以上传您的项目解决方案并分享它...
  • 从link获取它

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


【解决方案1】:

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

@model DesignationDemo.Models.Designation

仅添加 bundles.config 是不够的...

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

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

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

前面忘了...

最重要的代码

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

【讨论】:

  • 请再次检查问题我已添加模型和视图以供您进一步分析..
  • 我找不到@Scripts.Render("~/bundles/jqueryval"),你能查一下吗? ...我修改了我的消息,后来...我之前忘记了...
猜你喜欢
  • 2011-03-10
  • 2013-11-23
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
相关资源
最近更新 更多