MVC 3 Ajax.ActionLink 不起作用

发布于 2024-11-08 03:55:09 字数 2215 浏览 0 评论 0原文

我正在使用一些基于 MvcMusicStore 示例的商店,并且在使用 MVC3 Ajax.ActionLink / Ajax.RouteLink 帮助程序时遇到一些问题。问题是它根本不生成 Ajax 请求 (Request.IsAjaxRequest() == false)。我使用 Ajax.BeginForm / Ajax.BeginRouteForm 生成的表单运行良好。

配置:

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

脚本:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.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 src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

链接:

@Ajax.ActionLink("show cart", "Show", "Cart", new AjaxOptions() { OnSuccess = "handleSuccess", HttpMethod = "Get", OnFailure = "handleFailure" })

生成 HTML:

<a data-ajax="true" data-ajax-failure="handleFailure" data-ajax-method="Get" data-ajax-success="handleSuccess" href="/Cart/Show">show cart</a>

如上所述,这工作得很好:

@using (Ajax.BeginForm(
        "Show",
        new { controller = "Cart" },
        new AjaxOptions
        {
            OnSuccess = "handleSuccess",
            OnFailure = "handleFailure"
        }))
    {
        <input type="submit" class="button" />
    }

操作如下所示:

[Authorize]     
public ActionResult Show()
{
    if (Request.IsAjaxRequest())
    {
        ViewBag.CartItems = ShoppingCart.GetCart(this)
            .Items;

        return Json(new AjaxResultViewModel()
        {
            Content = RenderPartialViewToString(),
            UpdateTargetSelector = "#dialog",
            InsertionMode = InsertionMode.InsertBefore
        }, JsonRequestBehavior.AllowGet);
    }

    ViewBag.Exception = new NotSupportedException();
    return View("Error");
}

我已经搜索了一段时间,找不到此行为的原因,也许有人可以帮助我?

此致!

I'm playing around with the some MvcMusicStore example based shop and having some problems with the MVC3 Ajax.ActionLink / Ajax.RouteLink helpers. The problem is that it simply does not generate an Ajax request (Request.IsAjaxRequest() == false). The forms I'm generating using the Ajax.BeginForm / Ajax.BeginRouteForm are working nicely though.

Config:

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

Scripts:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.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 src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Link:

@Ajax.ActionLink("show cart", "Show", "Cart", new AjaxOptions() { OnSuccess = "handleSuccess", HttpMethod = "Get", OnFailure = "handleFailure" })

Generate HTML:

<a data-ajax="true" data-ajax-failure="handleFailure" data-ajax-method="Get" data-ajax-success="handleSuccess" href="/Cart/Show">show cart</a>

As said, this works just fine:

@using (Ajax.BeginForm(
        "Show",
        new { controller = "Cart" },
        new AjaxOptions
        {
            OnSuccess = "handleSuccess",
            OnFailure = "handleFailure"
        }))
    {
        <input type="submit" class="button" />
    }

The action looks like this:

[Authorize]     
public ActionResult Show()
{
    if (Request.IsAjaxRequest())
    {
        ViewBag.CartItems = ShoppingCart.GetCart(this)
            .Items;

        return Json(new AjaxResultViewModel()
        {
            Content = RenderPartialViewToString(),
            UpdateTargetSelector = "#dialog",
            InsertionMode = InsertionMode.InsertBefore
        }, JsonRequestBehavior.AllowGet);
    }

    ViewBag.Exception = new NotSupportedException();
    return View("Error");
}

I've been searching for a while now and couldn't find the reason for this behavior, maybe someone could help me out?

Best regards!

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

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

发布评论

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

评论(3

最好是你 2024-11-15 03:55:09

在靠近底部的 _layout.cshtml 上的新 MVC4 模板中,有一个 @Scripts.Render("~/bundles/jquery")

我必须包括
@Scripts.Render("~/bundles/jqueryval") 这是 Ajax 库

这解决了我使用索引页进行完整回发时遇到的问题。我的操作链接代码:

<div id="timeDisplay">
@Ajax.ActionLink("Click to display server time", "Time", new AjaxOptions { HttpMethod="GET",UpdateTargetId="timeDisplay"});
</div>

In a new MVC4 Template on the _layout.cshtml close to the bottom, there is a @Scripts.Render("~/bundles/jquery").

I had to include
@Scripts.Render("~/bundles/jqueryval") which is the Ajax library

This fixed the problem I was having with a full postback using the index page. My action link code:

<div id="timeDisplay">
@Ajax.ActionLink("Click to display server time", "Time", new AjaxOptions { HttpMethod="GET",UpdateTargetId="timeDisplay"});
</div>
抚笙 2024-11-15 03:55:09

我通过按以下确切顺序声明以下脚本来使其工作:

@section script
{
  <script src="@Url.Content("~/Scripts/MicrosoftAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript">   </script>
}

为了完整起见,在我的 *_Layout.cshtml* 文件中,我声明了以下内容:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

@RenderSection("script", required:false)

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>

I made it work by declaring the following scripts in this exact order:

@section script
{
  <script src="@Url.Content("~/Scripts/MicrosoftAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>
  <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript">   </script>
}

For the sake of completeness, in my *_Layout.cshtml* file I declared the following:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

@RenderSection("script", required:false)

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
友谊不毕业 2024-11-15 03:55:09

也许[这篇文章]有答案。

简而言之:js 函数“handleSuccess”和“handleFailure”是否可以通过表单访问

LG
瓦拉帕

Maybe [ this post ] has the answer.

In short: Are the js functions "handleSuccess" and "handleFailure" accessible by the form?

Lg
warappa

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