【问题标题】:ActionLink to HttpPost action methodActionLink 到 HttpPost 操作方法
【发布时间】:2012-11-25 04:24:57
【问题描述】:

我的页面上有这个链接:

@Html.ActionLink("Like", "Like", "Like", new {likeId = i.ItemId}, new {id = @i.ItemId, @class = "likeButton"})

这是我的 ajax 调用:

$(document).on("click", ".likeButton", function (event) {

        var itemId = event.target.id;

        $.ajax({
            url: this.href,
            type: 'POST',
            data: { item: itemId },
            context: this,
            success: function (result) {
                ...
        return false;
    });

当动作方法如下时它可以工作:

public ActionResult Like(int itemId)
...

但是如果我用[HttpPost] 装饰方法,它就不起作用了。
这可以实现吗?
如果我不添加[HttpPost],还有什么安全问题?

【问题讨论】:

  • 你在寻找 'int itemId',为什么你要传递 item: 'data: { item: itemId }' ?

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


【解决方案1】:

试试这个:

$(document).on("click", ".likeButton", function (event) {

    $.ajax({
        url: this.href,
        type: 'POST',
        context: this,
        success: function (result) {
            ...
    return false;
});

您将 item.Id 传递两次,第一次在 url 中,第二次在正文中。使用 post 方法时,仍然可以通过 url 传递参数。当你想隐藏这些参数时,用 body 传递参数是很好的。

还有一件事,你可以在这种情况下使用 Ajax.ActionLink(因为它是为这种情况创建的)

你错了:

data: { item: itemId } 

应该是:

data: { itemId: itemId },

【讨论】:

  • 是的,当我从函数中删除“数据”时问题就消失了。
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多