I need to send some other data, ideally wrapped in a JSON object, down to the client. As well as that however I need to send a Partial view. Now I can only think of two ways:
- Send JSON object and then make a开发者_StackOverflow社区nother call to load contents of partial view into div.
- Send HTML for Partial View as a property of the JSON object and then load it into div.
How would I go about doing the second option? Is there a command to render partial views into a string? Or is the first approach better?
Cheers, Damien
Is there a command to render partial views into a string
No direct way as I know of. But it's possible with some mocking. I use Moq here, but any mocking framework would do.
var writer = new StringWriter();
var context = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
context.Setup(ctx => ctx.Response).Returns(response.Object);
response.Setup(res => res.Output).Returns(writer);
var oldContext = ControllerContext.HttpContext;
ControllerContext.HttpContext = context.Object;
var partialView = PartialView("TagCloud", tags);
partialView.ExecuteResult(ControllerContext);
var resultHtml = writer.ToString();
ControllerContext.HttpContext = oldContext;
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论