Ajax.ActionLink alternative with mvc core

前端 未结 4 1930
时光取名叫无心
时光取名叫无心 2021-01-13 06:03

In MVC5 there is @Ajax.ActionLink that is useful to update just a partial view instead of reloading the whole View. Apparently in MVC6 is not supported anymore.

4条回答
  •  轮回少年
    2021-01-13 06:32

    ViewComponent's are not replacement of ajaxified links. It works more like Html.Action calls to include child actions to your pages (Ex : Loading a menu bar). This will be executed when razor executes the page for the view.

    As of this writing, there is no official support for ajax action link alternative in aspnet core.

    But the good thing is that, we can do the ajaxified stuff with very little jQuery/javascript code. You can do this with the existing Anchor tag helper

    Update
    

    In the javascript code, just listen to the link click and make the call and update the DOM.

    $(function(){
    
       $("#aUpdate").click(function(e){
    
         e.preventDefault();
         var _this=$(this);
         $.get(_this.attr("href"),function(res){
             $('#'+_this.data("target")).html(res);
         });
    
       });
    
    });
    

    Since you are passing the parameter in querystring, you can use the jQuery load method as well.

    $(function(){
    
       $("#aUpdate").click(function(e){
    
         e.preventDefault();
         $('#' + $(this).data("target")).load($(this).attr("href"));
    
       });
    
    });
    

提交回复
热议问题