简体   繁体   English

将HTML表从控制器传递到MVC视图

[英]Passing HTML Table to MVC View from controller

I have an MVC application and I would like to pass an html table from the controller to the view using json. 我有一个MVC应用程序,我想使用json将控制器中的html表传递给视图。

I found this Q&A in this site: Sending HTML Code Through JSON which recommends json_encode but it's for PHP and I'm using C#... 我在这个网站上找到了这个问答: 通过JSON发送HTML代码推荐使用json_encode,但它适用于PHP而我正在使用C#...

How can I pass an HTML Table using Json? 如何使用Json传递HTML表?

Currently I tried: 目前我试过:

return Json(MyHTMLTable);

In my view: 在我看来:

<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        url: '@Url.Action("GetTable","MyPages")',
        type: 'POST',
        success: function (result) { $('#ph').html(result.responseText); },
        error: function (result) {
            $('#err').html(result.responseText);
        }
    })
});

(ph is a div) (ph是div)

I got the following error: A circular reference was detected while serializing an object of type 'System.Web.UI.WebControls.TableRow'. 我收到以下错误: 序列化“System.Web.UI.WebControls.TableRow”类型的对象时检测到循环引用。

Change your ajax to remove the @URL.Action 更改您的ajax以删除@URL.Action

$.ajax({
    url: 'MyPages/GetTable',
    type: 'POST',
    success: function (result) { $('#ph').html(result.responseText); },
    error: function (result) {
        $('#err').html(result.responseText);
    }
})

in your ActionResult() return a PartialView() ? 在你的ActionResult()返回一个PartialView()

In your partial view named TableHtml in this example will be your html 在你的部分视图中,这个例子中的TableHtml将是你的html

public ActionResult GetTable(){

     // do something

     return PartialView("TableHtml");


}

Ajax will accept this Ajax会接受这个

However if you must return HTML then you can wrap in a Json 但是,如果必须返回HTML,则可以包装Json

public ActionResult GetTable(){

     // do something
     String myHtml = "<div></div>;

    return Json(myHtml, JsonRequestBehaviour.AllowGet);
}

First of all, there is no need to pass an HTML table from a controller to a view using JSON. 首先,不需要使用JSON将HTML表从控制器传递到视图。 JSON is meant as a way to transmit stringified data, usually between web services and clients. JSON是一种传输字符串化数据的方式,通常是在Web服务和客户端之间传输。 It is similar in function to SOAP. 它在功能上与SOAP类似。 Transmitting data from your controller to your view in this manner is bad because you lose the strongly-typing that passing with a model affords you. 以这种方式将数据从控制器传输到您的视图是不好的,因为您失去了通过模型传递给您的强类型。

So, what you can do is simply pass your HTML table as a variable to your view in a view model. 因此,您可以做的只是将HTML表作为变量传递给视图模型中的视图。 You could also use ViewBag, but you lose strong typing this way. 您也可以使用ViewBag,但这种方式会失去强大的输入。 Either way, assuming that the table is generated in your C#, you simply need to convert the string containing the HTML to a MvcHtmlString . 无论哪种方式,假设表是在C#中生成的,您只需要将包含HTML的字符串转换为MvcHtmlString You do it like this: 你这样做:

var table = MvcHtmlString.Create("<table>...");

and then either include this in your model or in the ViewBag. 然后将其包含在您的模型或ViewBag中。 You can then on your view page use classic Razor syntax to print the table to the page: 然后,您可以在视图页面上使用经典的Razor语法将表打印到页面:

@Model.Table

or 要么

@ViewBag.table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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