MVC 3 Ajax.ActionLink 不起作用
我正在使用一些基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在靠近底部的
_layout.cshtml
上的新 MVC4 模板中,有一个@Scripts.Render("~/bundles/jquery")
。我必须包括
@Scripts.Render("~/bundles/jqueryval")
这是 Ajax 库这解决了我使用索引页进行完整回发时遇到的问题。我的操作链接代码:
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 libraryThis fixed the problem I was having with a full postback using the index page. My action link code:
我通过按以下确切顺序声明以下脚本来使其工作:
为了完整起见,在我的 *_Layout.cshtml* 文件中,我声明了以下内容:
I made it work by declaring the following scripts in this exact order:
For the sake of completeness, in my *_Layout.cshtml* file I declared the following:
也许[这篇文章]有答案。
简而言之: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