【问题标题】:Is it possible to pass multiple lines of HTML as a string argument to a HTML helper?是否可以将多行 HTML 作为字符串参数传递给 HTML 帮助程序?
【发布时间】:2012-05-13 19:34:27
【问题描述】:

我的用例是我有一个 html 模板,我希望编译服务器端,但在 RAZOR 视图中定义。

无法使用 RAZOR 语法渲染此模板,因为该值仅存在于客户端。像jQuery模板之类的。

理想情况下,它会从 tje 视图中传入:

@Html.Template(<this somehow indicates it's the start of my string>
    <div>Hello {{= NameOfHomePlanet }}</div>
    <div>And welcome to a second line!</div>
</this somehow indicates it's the end of my string>);

现在在助手中:

public static Template(this Html html, string template) 
{
    // tmplate == "    <div>Hello {{= NameOfHomePlanet }}</div>\n"
    // + "<div>And welcome to a second line!</div>";
}

这将导致最外层虚构标签之间的 HTML 作为名为 Template 的 Html 帮助器的第一个参数传入。

在玩的过程中,我无法弄清楚语法。这可能吗?

【问题讨论】:

  • HTML 有预定义的结构?我建议您为每次需要使用不同的 HtmlHelper。比方说,一个用于处理“PlanetWelcome”的 HtmlHelper,您将“NameOfHomePlanet”和任何其他信息作为参数传递。

标签: c# asp.net-mvc-3 razor html-helper


【解决方案1】:

我不建议这样做,但你当然可以

@{
  string someHtml = "<div>" + model.NameOfHomePlanet + "</div>";
}

@Html.CustomHelperTemplate(someHtml)

除此之外很难说,因为您的问题非常模糊。

【讨论】:

  • 我将更新主题以更具体。我可以看出它是多么模糊。
猜你喜欢
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多