4

The Setup:

I have updated an app from ASP.NET MVC 3 to ASP.NET MVC 4.

The app worked fine in MVC 3. The only thing that isn't working in MVC 4 is Ajax.Begin form: the form defaults to full page requests, instead of async AJAX requests.

Essentially, it is a wizard that I have written, but that is irrelevant. Model.Step.ActionName correctly returns a string (see code below).

The Code:

The code in the View is:

@{ 
    using (Ajax.BeginForm(Model.Step.ActionName, null, new AjaxOptions { UpdateTargetId = "wizardStep", OnBegin="isValid", LoadingElementId="PleaseWait", OnSuccess="SetFocusOnForm" }, 
        new {@name="wizardForm", @id="wizardForm" } ))
{

//form contents

}
}

The Rendering:

I note that Ajax.BeginForm in MVC 4 uses HTML 5. I show the difference between how MVC 3 and MVC 4 render the form below:

MVC 3:

<form action="/Solicitors/Company/New/YourDetails" id="wizardForm" method="post" name="wizardForm" onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));" onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, loadingElementId: 'PleaseWait', updateTargetId: 'wizardStep', onBegin: Function.createDelegate(this, isValid), onSuccess: Function.createDelegate(this, SetFocusOnForm) });">

// form contents

</form>

MVC 4:

<form action="/Solicitors/Company/New/LoginDetails" data-ajax="true" data-ajax-begin="isValid" data-ajax-loading="#PleaseWait" data-ajax-mode="replace" data-ajax-success="SetFocusOnForm" data-ajax-update="#wizardStep" id="wizardForm" method="post" name="wizardForm" novalidate="novalidate">

// form contents

</form>

I have no idea if this is correct, but assume it is.

The Problem:

The problem, however, is that Ajax isn't used, just full page refreshes. So sumat is wrong...

The Question:

The question is: What is wrong?!

4

1 回答 1

12

好的,我已经解决了这个问题。

在 MVC 3 应用程序中,我在 web.config 中注释掉了以下内容:

<appSettings>
    <add key="webpages:Version" value="1.0" />
    <!--<add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />-->
  </appSettings>

这解释了为什么 asp.net mvc 3 没有呈现 html 5。

在新的 mvc 4 app 中,默认设置有ClientValidationEnbled=trueUnobstrusiveJavaScriptEnabled=true,如下:

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    ...
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

所以我的应用需要包含以下 javascript 文件:

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

它需要丢弃 microsoft*.js 文件,即:

@*<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script>*@

在阅读了@Darin Dimitrov 对以下问题的回复后,我明白了这一点:

MicrosoftAjax.js、MicrosoftMvcAjax.js 和 MicrosoftMvcValidation.js 从 ASP.NET MVC 3 开始是否已过时?

非常感谢达林。答案值得一读,让自己了解我禁用的两个 appSettings 的含义。

于 2012-09-20T13:15:51.647 回答