将HTML从控制器传递到视图

时间:2022-06-23 12:25:45

I've made a structure to retrieve from database, based on the role given, to return menu items.

我已经建立了一个结构,根据给定的角色从数据库中检索返回菜单项。

Now, I need to make a recursive method to render this HTML in the controller and pass this HTML to view. But i just don't know how to write native HTML in the controller. Any suggestions?

现在,我需要创建一个递归方法来在控制器中呈现此HTML并将此HTML传递给视图。但我只是不知道如何在控制器中编写本机HTML。有什么建议?

4 个解决方案

#1


The controller should not handle any of the HTML at all. That's what the view in MVC is for. Your controller should pass a data structure from the model, and the view should render that data structure as HTML.

控制器根本不应该处理任何HTML。这就是MVC中的观点。您的控制器应该从模型传递数据结构,视图应该将该数据结构呈现为HTML。

#2


Easier than you might think:

比你想象的更容易:

Controller:

ViewData["html"] = @"<a href='http://www.*.com'>Stack Overflow</a>";

View:

<%= ViewData["html"] %>

I do agree this isn't the best method. I would suggest you write the html markup in your view and substitute the values from your model instead.

我同意这不是最好的方法。我建议你在视图中编写html标记,然后替换模型中的值。

e.g.

Controller:

ViewData["Title"] = "Stack Overflow";
ViewData["Url"] = @"http://www.*.com";

View:

<a href="<%=Html.Encode( ViewData["Url"] )%>">
     <%=Html.Encode( ViewData["Title"] )%></a>

If you have to create many, you could use a partial view / user control to encapsulate the common markup.

如果必须创建许多,则可以使用部分视图/用户控件来封装公共标记。

#3


It's definitely not the idea of MVC (or whatever you're doing) to render HTML in the Controller. HTML has to be handled in the view. What if you want to provide an alternative UI (e.g. Windows Application)? HTML does not really fit into an WinApp.

在Controller中渲染HTML绝对不是MVC(或者你正在做的任何事情)的想法。必须在视图中处理HTML。如果您想提供替代UI(例如Windows应用程序),该怎么办? HTML并不适合WinApp。

#4


You can use an HTML helper to create the HTML. For example, we have a Menu system that is very complex, so we create an HtmlHelper extension. Here's a simple html extension (I use TagBuilders to make the HTML easier ... very handy when you have lots of nested tags).

您可以使用HTML帮助程序来创建HTML。例如,我们有一个非常复杂的Menu系统,因此我们创建了一个HtmlHelper扩展。这是一个简单的html扩展(我使用TagBuilders使HTML更容易......当你有很多嵌套标签时非常方便)。

  public static String MyNewExtension(this HtmlHelper html, String extraVariable)
  {
        if (html == null)
        {
            throw new ArgumentNullException("html");
        }

        TagBuilder h1Tag = newTagBuilder("h1");
        h1Tag.InnerHtml = extraVariable;
        return h1Tag.ToString();
  }

#1


The controller should not handle any of the HTML at all. That's what the view in MVC is for. Your controller should pass a data structure from the model, and the view should render that data structure as HTML.

控制器根本不应该处理任何HTML。这就是MVC中的观点。您的控制器应该从模型传递数据结构,视图应该将该数据结构呈现为HTML。

#2


Easier than you might think:

比你想象的更容易:

Controller:

ViewData["html"] = @"<a href='http://www.*.com'>Stack Overflow</a>";

View:

<%= ViewData["html"] %>

I do agree this isn't the best method. I would suggest you write the html markup in your view and substitute the values from your model instead.

我同意这不是最好的方法。我建议你在视图中编写html标记,然后替换模型中的值。

e.g.

Controller:

ViewData["Title"] = "Stack Overflow";
ViewData["Url"] = @"http://www.*.com";

View:

<a href="<%=Html.Encode( ViewData["Url"] )%>">
     <%=Html.Encode( ViewData["Title"] )%></a>

If you have to create many, you could use a partial view / user control to encapsulate the common markup.

如果必须创建许多,则可以使用部分视图/用户控件来封装公共标记。

#3


It's definitely not the idea of MVC (or whatever you're doing) to render HTML in the Controller. HTML has to be handled in the view. What if you want to provide an alternative UI (e.g. Windows Application)? HTML does not really fit into an WinApp.

在Controller中渲染HTML绝对不是MVC(或者你正在做的任何事情)的想法。必须在视图中处理HTML。如果您想提供替代UI(例如Windows应用程序),该怎么办? HTML并不适合WinApp。

#4


You can use an HTML helper to create the HTML. For example, we have a Menu system that is very complex, so we create an HtmlHelper extension. Here's a simple html extension (I use TagBuilders to make the HTML easier ... very handy when you have lots of nested tags).

您可以使用HTML帮助程序来创建HTML。例如,我们有一个非常复杂的Menu系统,因此我们创建了一个HtmlHelper扩展。这是一个简单的html扩展(我使用TagBuilders使HTML更容易......当你有很多嵌套标签时非常方便)。

  public static String MyNewExtension(this HtmlHelper html, String extraVariable)
  {
        if (html == null)
        {
            throw new ArgumentNullException("html");
        }

        TagBuilder h1Tag = newTagBuilder("h1");
        h1Tag.InnerHtml = extraVariable;
        return h1Tag.ToString();
  }