简体   繁体   中英

Passing data from Partial View to its parent View in asp.net core mvc

This link have a similar answer which works fine but some changes required above answer works fine, but my objective is a little bit different, I wanted to pass the variable from partial view who contains image URL, I want to use that URL in the main view somewhere. the scenario is, calling ajax to load the partial view and displaying in modal, partial view having a lot of ajax which calling controller for fetching folder and files from the server, on the response of ajax I got an image file URL in the partial view, now I want to pass that URL into the main view, how to do that?


the main view -> works fine enter code here

$(document).ready(function (){
          $("#button1").click(function ()    
           {
                    $("#modalbodypopup").load("/ControllerName/GetPartial");
                     jQuery.noConflict();
                     $('#imagePopup').modal('show');      
           });   
   });
</script>

controller -> works fine enter code here

       public IActionResult GetPartial()     
       {
            return PartialView("~/Views/Shared/_FileManager.cshtml");
       }

partial view calling different controller who returning HTML who have a lot of link and folders name and image file name now on the calling of ajax from the partial view, after a successful response, I got a file URL in the variable inside partial view javascript side which I want to pass to the main view because I am displaying that image there.

There are a couple of ways you can do this. Your GetPartial() method can do:

return PartialView("_FileManager");

And as long as the file is in the shared folder, MVC works out the path.

You can also return the contents of a file directly:

var file = File.ReadAllBytes(..);
return File(file, "image/png");

This allows you to stream the image url directly into the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM