Search code examples
c#javascriptjqueryasp.net-mvcjson

How to show json result (which is partial view) as a modal pop up?


I am trying to show a modal pop up by making a getjson call like this from my javascript:

 $.getJSON('/Home/somefunction', function(result) {
       var toto = $(result);
       alert(toto);
       });

      public ActionResult somefunction()
      {
         return new JsonResult { Data = PartialView("modal pop up partial view") };
      }

My modal pop up view is partialview.

  1. I don't know if this is right way to convert partial view into jsonresult inside my controller function.
  2. I dont know, how to use the result in getjson to show was pop up(do I need to assign to any div)?

Thanks


Solution

  • The better way is to return a PartialView from the controller. Use $.get or $.ajax to make a GET request to the server (or a POST if you need), and the callback will have the HTML. You can then inject the HTML into the parent view (inside the modal).