Ajax.BeginForm让我发疯

时间:2010-12-26 05:52:33

标签: asp.net-ajax asp.net-mvc-3 ajax.net

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" />
}

行动:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Customer(CustomerRequestModel customer)
{
  // ...
}

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

this.Request.IsAjaxRequest()
false

为什么?!

1 个答案:

答案 0 :(得分:11)

如果你有

,你应该包括jquery.unobtrusive-ajax.js
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

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

有关Brad Wilson

的不显眼的Ajax的更多信息