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.

    评论

报告相同问题?