Returning a PartialView with both HTML and JavaScript

后端 未结 4 465
终归单人心
终归单人心 2021-01-06 15:43

I am making an AJAX call (with jQuery) to retrieve a PartialView. Along with the HTML I\'d like to send back a JSON representation of the object the View is dis

4条回答
  •  失恋的感觉
    2021-01-06 16:22

    The same solution as Andrew, but maybe a bit more MVC'ish...

    Create a new class which inherits from ViewPage. Override its Render method to render the page itself, and then stuff that into the output Andrew suggested. So that all the hacking occurs in the Render method, where it should happen.

    Now each view that you create you change the line:

    <%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    

    with

    <%@ Page Title="" Language="C#" Inherits="CustomViewPage" %>
    

    I didn't check it myself, but guess it should work. If you want I can try and build it.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题