ASP.NET MVC 4 - Ajax.BeginForm和HTML5MVC、NET、ASP、BeginForm

2023-09-11 00:39:59 作者:。我们都一样

我已经更新从ASP.NET MVC 3的应用程序,以ASP.NET MVC 4。

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

该应用程序的MVC 3不工作的MVC 4唯一的罚款是Ajax.Begin形式:形式默认为整页的请求,而不是异步AJAX请求。

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.

从本质上讲,这是我写的一个向导,但是这是无关紧要的。 Model.Step.ActionName正确返回一个字符串(见下文code)。

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

在code的看法是:

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

//form contents

}
}

渲染:

我注意到,Ajax.BeginForm在MVC 4使用HTML 5,我展示的MVC 3和MVC 4如何呈现以下表单的区别是:

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.

但问题是,阿贾克斯没有使用,只是整页刷新。所以sumat是错误的...

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

现在的问题是:?!什么是错的。

The question is: What is wrong?!

推荐答案

好了,我已经解决了这个。

OK, I have solved this.

在MVC 3应用程序,我已经注释掉在web.config如下:

In the MVC 3 app, I had commented out the following in the 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。

This explains why asp.net mvc 3 was not rendering html 5.

在新的MVC 4应用程序,默认设置具有 ClientValidationEnbled = TRUE UnobstrusiveJavaScriptEnabled = TRUE ,如下

In the new mvc 4 app, the default setting has ClientValidationEnbled=true and UnobstrusiveJavaScriptEnabled=true, as follows:

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

所以,我的应用程序所需的下列JavaScript文件应包括:

So my app needed the following javascript files to be included:

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

和它需要微软的* .js文件扔掉,即:

And it needed the microsoft*.js files throwing away, ie:

@*<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季米特洛夫回答以下问题后:

I figured this out after reading @Darin Dimitrov's reply to the following question:

是MicrosoftAjax.js,MicrosoftMvcAjax.js和MicrosoftMvcValidation.js过时ASP.NET MVC 3?

非常感谢达林。答案是值得一读,开导自己作为两个的appSettings我已经禁用的含义。

Many thanks to Darin. The answer is worth reading, to enlighten yourself as to the meaning of the two appSettings I had disabled.