Ajax.BeginForm 让我抓狂

发布于 2024-10-09 19:41:30 字数 1833 浏览 0 评论 0原文

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

为什么?!

ASP.NET MVC3

I have a partial view that is initially rendered inside a div. The following is the partial code:

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

The action:

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

In the immediate window, this is what I get:

this.Request.IsAjaxRequest()
false

Why?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-10-16 19:41:30

如果您的 web.config 文件中有 jquery.unobtrusive-ajax.js,则应包含 jquery.unobtrusive-ajax.js

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

,该文件由 MVC3 RC2 默认启用。
并且还要删除最后 3 个(MicrosoftAjax、MicrosoftMvcAjax 和 MicrosoftMvcValidation)脚本引用,因为在使用非侵入式版本时不需要它们。

有关不显眼 Ajax 的更多信息,作者: Brad Wilson

You should include jquery.unobtrusive-ajax.js if you have

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

in your web.config file, which is default enabled by MVC3 RC2.
And also delete the last 3 (MicrosoftAjax, MicrosoftMvcAjax and MicrosoftMvcValidation)script references, because you don't need them when using unobtrusive version.

More info about unobtrusive Ajax by Brad Wilson

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文