Ajax.BeginForm让我发疯

Fab*_*iro 2 asp.net-ajax ajax.net asp.net-mvc-3

ASP.NET MVC3

我有一个局部视图,最初在div中呈现.以下是部分代码:

@model Venue.Models.Validation.CustomerRequestModel

<script src="@Url.Content("~/Scripts/jquery-1.4.4.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>

<script type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/Scripts/MicrosoftMvcAjax.js"></script>
<script type="text/javascript" src="/Scripts/MicrosoftMvcValidation.js"></script>

@{ Html.RenderPartial("Message"); }
@Html.ValidationSummary()

@using (Ajax.BeginForm(
            "Customer",
            "Service",
            null,
            new AjaxOptions()
            {
                HttpMethod = "post",
                InsertionMode = InsertionMode.Replace,
                LoadingElementDuration = 100,
                LoadingElementId = "loading-customer",
                OnBegin = "hideSubmitButton",
                OnSuccess = "hideForm",
                OnComplete = "showSubmitButton",
                OnFailure = "showErrorMessage",
                UpdateTargetId = "formclientes",
            },
            new
            {
                id = "customer-form"
            }))
{
    // Fields are all type="text" although some are numbers.
    <input type="text" name="Address" class="clientes_form" />
}
Run Code Online (Sandbox Code Playgroud)

那个行动:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Customer(CustomerRequestModel customer)
{
  // ...
}
Run Code Online (Sandbox Code Playgroud)

在即时窗口中,这是我得到的:

this.Request.IsAjaxRequest()
false
Run Code Online (Sandbox Code Playgroud)

为什么?!

小智 11

如果有的话,你应该包括jquery.unobtrusive-ajax.js

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
Run Code Online (Sandbox Code Playgroud)

在您的web.config文件中,MVC3 RC2默认启用该文件.并且还删除了最后3个(MicrosoftAjax,MicrosoftMvcAjax和MicrosoftMvcValidation)脚本引用,因为在使用不显眼的版本时不需要它们.

有关更多信息Brad Wilson的 unobtrusive Ajax