title: How to load partialview content and return to ajax result?
body: <p>I use Ajax :</p>
  
  <pre><code>$.ajax({
              url: url,
              data: request,
              dataType: "json",
              success: function (data) {
  
                  $(".ad-image-wrapper").html(data.html);
              },
              error: function () {
  
              }
          });
  </code></pre>
  
  <p>and I want to load the whole html from a partial view that take some data from LoadPictureGallery. Anyway to write this properly ?</p>
  
  <pre><code> public ActionResult LoadPictureGallery(string xxx)
      {
          var model = List&lt;ABCClass&gt;(){ blah blah};
          return new JsonResult() { html= XXXPartialView(model), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
      }
  </code></pre>
  
  <p>UPDATED: What I need is <a href="https://stackoverflow.com/questions/483091/render-a-view-as-a-string">Render a view as a string</a> </p>
  
  <p>render a partialview to string and return it.</p>
  
  <pre><code> public ActionResult LoadPictureGallery(string url, string alt)
      {
  
          var picture = new PictureModel()
          {
              ImageUrl = url,
              AlternateText = alt,
              FullSizeImageUrl = url,
              Title = alt
          };
          return new JsonResult() { Data = RenderRazorViewToString("_PhotoItem", picture), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  
  
      }
  
  public string RenderRazorViewToString(string viewName, object model)
      {
          ViewData.Model = model;
          using (var sw = new StringWriter())
          {
              var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
              var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
              viewResult.View.Render(viewContext, sw);
              viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
              return sw.GetStringBuilder().ToString();
          }
      }
  </code></pre>