weixin_33701617 2015-08-21 16:35 采纳率: 0%
浏览 14

Ajax.ActionLink不发布

I have run in to a problem with an ajax.actionlink

My code is:

@Ajax.ActionLink("Delete", "DeleteCategory", new { id = item.ID }, new AjaxOptions
               {
                   HttpMethod = "POST",
                   OnFailure = "function() { alert('fail'); }",
                   OnSuccess = "function() { alert('success'); }"
               })

and the controller is

 [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteCategory(int id)
        {

            categoryBLL.DeleteCategory(id);
            return RedirectToAction("CreateCategory");

        }

But for some reason I can not get it to work, it doesn't get triggered... It only redirects to /DeleteCategory/Delete/{id}

Instead of just posting to the controller, and carrying that out..

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • weixin_33735676 2015-08-21 17:02
    关注

    first add the reference to the package MicrosoftMvcAjax.Mvc5 and next add the js to your page:

    Nuget Package MicrosoftMvcAjax

    and the js references:

    <script src="~/Scripts/MicrosoftAjax.js"></script>
    <script src="~/Scripts/MicrosoftMvcAjax.js"></script>
    

    Also, exists some problem when you use @ajax.actionlink with HTTP Post, with get all its fine, get:

     @Ajax.ActionLink("Read", "Read", new { id = 1 }, new AjaxOptions
    {
       HttpMethod = "Get",
       OnFailure = "function() { alert('fail'); }",
       OnSuccess = "function() { alert('success'); }"
    })
    
    [HttpGet]
    public ActionResult Read(int id)
    {
        return RedirectToAction("CreateCategory");
    }
    

    But, for POST use the helper: @Ajax.BeginForm() or a library like jQuery.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器