0

我需要像这样的字符串渲染视图:

public class Controller : System.Web.Mvc.Controller
{
    public ViewResult ViewList()
    {
        string html = "<div id=\"contentView\">";
        html += Title(null);
        html += GridControls(null);
        html += FilterControls(null);
        html += Grid(null, new Sibi.Seg.User().List(pagina));
        html += "</div>";
        return View(html );
    }

}

然后我需要在 Controller 中的 return View() 上渲染这个字符串,看看它:

public ActionResult Index()
{
      return ViewList();
}
4

1 回答 1

1

您可以使用控制器的Content方法。您的代码将如下所示:

public ActionResult ViewList()
{
    string html = "<div id=\"contentView\">";
    html += Title(null);
    html += GridControls(null);
    html += FilterControls(null);
    html += Grid(null, new Sibi.Seg.User().List(pagina));
    html += "</div>";
    return Content(html, "text/html");
}

或者您可以将内部代码移动到静态类并从 mvc 视图调用静态方法,例如:

@{
    Layout = null;
}
<div id="contentView">
    @(Html.Raw(StaticMethods.Title(null)))
    @(Html.Raw(StaticMethods.GridControls(null)))
    @(Html.Raw(StaticMethods.FilterControls(null)))
    @(Html.Raw(StaticMethods.Grid(null, new Sibi.Seg.User().List(pagina))))
</div>

这两种解决方案中的任何一种都将导致相同的结果。

于 2013-11-12T18:29:10.177 回答