【问题标题】:Ajax.BeginForm driving me crazyAjax.BeginForm 让我发疯
【发布时间】:2011-05-30 19:05:05
【问题描述】:

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

为什么?!

【问题讨论】:

  • 嗨法比奥...我使用您的代码,但在操作中我尝试更改客户属性,例如:customer.Name = "NewName" 并返回视图(客户),但 UI没有更新...你知道为什么吗?
  • 在这种情况下,我使用 Ajax.BeginForm 而不是 Html.BeginForm,因为我想返回一个局部视图(这样只有页面的一部分会被更新)。如果你的目标是一样的,你应该这样返回:return this.PartialView("PartialViewName", customer);。如果这不能回答您的问题,请发布一个新问题并为我提供访问它的链接。

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


【解决方案1】:

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

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

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

Brad Wilson 提供的关于不显眼的 Ajax 的更多信息

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2012-03-14
    • 2021-04-20
    相关资源
    最近更新 更多