MVC3 Razor Ajax.ActionLink不会使用POST方法 [英] MVC3 Razor Ajax.ActionLink won't use POST method

查看:129
本文介绍了MVC3 Razor Ajax.ActionLink不会使用POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个链接的页面。这些链接应该做一个ajax帖子和回调。但是,该链接正在执行Get而不是Post。这会导致404错误,因为我没有操作方法来处理请求的URL获取。

I've got a page that contains multiple links. These links should do an ajax post and callback. However, the link is doing a Get instead of a Post. This causes a 404 error since I do not have an action method to handle a get at the requested URL.

如果我从Action方法中删除HTTPPost属性,则链接工作,但回调失败,我返回的Json将在新页面中呈现。

If I remove the HTTPPost attribute from my Action method, the link works, but the call back fails and the Json I return is rendered in a new page.

以下是我在视图中使用的代码。

Here is the code I'm using in my view.

<td id="action-@item.ItemID">@Ajax.ActionLink("Add", "AddToOrder", new { itemID = item.ItemID }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "actionCompleted" }, new { id = "add-" + item.ItemID })</td>

这最终会添加此HTML:

This ends up adding this HTML:

 <td id="action-012679"><a data-ajax="true" data-ajax-method="POST" data-ajax-success="actionCompleted" href="/mysite/neworder/AddToOrder?itemID=012679" id="add-012679">Add to Order</a></td>

我的控制器具有以下操作方法。

My Controller has the following Action Method.

[HttpPost]
public JsonResult AddToOrder(string itemID) {
    return Json(new { id = itemID, Action = "Added", "Just getting this working"});
}

我在Success上调用的回调方法如下:

My callback method that is called on Success looks like this:

<script language="javascript" type="text/javascript">        
    function actionCompleted(response, status, data) {
        alert("We have returned");
    }

</script>

如果我将动作方法的[HTTPPost]属性更改为[HTTPGet],我会收到Json错误。我可以通过将JsonRequestBehavior.AllowGet添加到我的返回值来解决这个问题,但这不会使用页面上定义的回调函数而失败。

If I change the [HTTPPost] attribute on my action method to [HTTPGet] I get an Json error. I can fix this by adding the JsonRequestBehavior.AllowGet to my return value, but this doesn't use the call back function defined on the page and fails.

任何帮助都会不胜感激。

Any help would be appreciated.

推荐答案

可能你没有附加到页面的jquery.unobtrusive-ajax.js脚本,链接正常降级为常规锚点。

Probably you don't have jquery.unobtrusive-ajax.js script attached to page and link is gracefully degraded to regular anchor.

这篇关于MVC3 Razor Ajax.ActionLink不会使用POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆