Ajax.BeginForm not working with Html.ValidationSummary

青春壹個敷衍的年華 提交于 2019-12-05 18:50:20

Make sure that you have included the jquery.unobtrusive-ajax.js script to your view after jquery itself. Otherwise the Ajax.BeginForm helper won't do what you think it does:

<script type="text/javascript" src="@Url.Content("~/scripts/jquery-YOUR-VERSION.js")"></script>
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.unobtrusive-ajax.js")"></script>

Where are your scripts added? In the _layout.cshtml or in the view itself? How are you loading the view? Is it with an ajax request to show a partial view?

If you are loading the partial view through ajax or as a partial view, maybe it could be that the partial view was not yet loaded in the jquery DOM model tree.

I would try the following. Change

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
</div>

to

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
</div>

or

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
    @Scripts.Render("~/bundles/jqueryval") //if you have a bundle for it
</div>

My advice is anyway to load the validate and unobtrusive scripts only when you need them and not in the _layout.cshtml page.

Also don't forget to enable the following appSettings in the web.config

    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!