问题解决了
基本上我习惯于使用 Internet 模板启动我的 ASP.NET MVC4 项目。但是这次我尝试使用基本模板。现在,我正在尝试手动添加 jQuery 验证:我已经安装了 NuGet 包 jQuery.validation 并将其包含在我的 bundleconfig 和 _layout 文件中。
这是我的捆绑配置:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
以下是我将它包含在 _layout 中的方式:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
以下是每个请求加载的 js 文件:
http://localhost:10815/Scripts/jquery-2.0.3.js
http://localhost:10815/Scripts/jquery.unobtrusive-ajax.js
http://localhost:10815/Scripts/jquery.validate.js
http://localhost:10815/Scripts/jquery.validate.unobtrusive.js
在我的控制器中,我检查 ModelState.IsValid 是否存在,如果不是,则返回带有模型的视图 - 我已经调试了该方法并确定 modelstate 有错误。
最后在我的视图文件中,我包括:
@Html.ValidationSummary()
这会产生以下 HTML 输出:
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
我的问题是当我控制器返回视图时验证摘要没有改变 - 我不知道有什么问题 - 我错过了什么?
问题已解决 - 我使用了现在已关闭的 ajax - 没有 ajax 也能正常工作。我猜验证摘要没有被重新加载,因此没有更新 html。