refreshing html.renderaction with ajax request

2019-04-29 23:42发布

问题:

I have div that renders image gallery on my page

        <div id="gallery">       
    @{
        Html.RenderAction("UserGallery");
     }

I have this function which runs on the completion of a successful upload of a new image

        function filesUploadOnSuccess(e) {
            function updateCart() {
                //var tdata = $(frm).serialize(); 
                // or your data in the format that will be used ??
                $.ajax({
                    type: "GET",
                    //data: tdata,
                    url : '@Url.Action("UserGallery")',
                    dataType: "json",
                    success: function (result) { success(result); }
                });
            };
        }

        function success(result) {
            $("#gallery").html(result);
        }

the problem is that the gallery div doesnt get updated.

回答1:

dateType should be "html", not "json", if your action returns a PartialViewResult:

public ActionResult UserGallery()
{
  // do something
  return PartialView();    
}

and

 $.ajax({
    url : '@Url.Action("UserGallery")',
    dataType: "html",
    success: function (result) { success(result); }
 });