Sunday, May 3, 2015

Ajax Action link in MVC 4

 @Ajax.ActionLink(
    "Attachemnts",
        "ShowAttachments",
    "Board",
 new { id = 1 },
    new AjaxOptions
    {
        UpdateTargetId = "result",
        InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
        HttpMethod = "GET"
    })



On Controller side we need to have Get method which shall return partial view,


 [HttpGet]
        public ActionResult ShowAttachments(int id)
        {
            ViewBag.UserName = HttpContext.User.Identity.Name;
            BoardAttachmentService srvBa = new BoardAttachmentService();
            List<BoardAttachment> baLst = srvBa.GetBoardAttachmentsByBoardId(id).ToList();
            List<BoardAttachmentVM> BoardAttVM = Mapper.Map<List<BoardAttachment>, List<BoardAttachmentVM>>(baLst).ToList();
            return PartialView("_ShowAttachments", BoardAttVM);
        }