简体   繁体   English

如何在C#中使用Code构建HTML

[英]How to build HTML from Code in C#

I need to create an HtmlHelper in my MVC application which renders out some nested and fairly complex UL/LI (tree-like) structures. 我需要在我的MVC应用程序中创建一个HtmlHelper,它呈现出一些嵌套且相当复杂的UL / LI(树状)结构。 The HtmlTextWriter and XmlTestWriter classes provide means to construct proper HTML, but they are forward-only thus making my job pretty difficult, since after you rendered a tag you don't have a reference to "parent" tag. HtmlTextWriterXmlTestWriter类提供了构造正确HTML的方法,但它们只是前向的,因此使我的工作非常困难,因为在您呈现标记之后,您没有对“parent”标记的引用。

XDocument and XElement classes were the next candidates I looked at, but they have been created for XML and not for HTML, which might result in no-so-valid HTML (self-closing tags etc.). XDocumentXElement类是我看过的下一个候选者,但是它们是为XML创建的,而不是为HTML创建的,这可能导致不那么有效的 HTML(自闭标签等)。

How would I go about this? 我该怎么做?

This alleged duplicate is not really a duplicate and doesn't answer my question at all. 这个被指控的副本实际上并不重复,根本不回答我的问题。 It is about building an HTML-Helper rendering a single Html-Tag. 它是关于构建一个HTML-Helper渲染单个Html-Tag。 This is not what I am after. 这不是我追求的。 I'd like to know how to build an entire DOM in C# code. 我想知道如何用C#代码构建一个完整的DOM。

I've wanted to do something like this before, and ended up playing around with an object structure that would represent elements, like a server side DOM, that could be manipulated in code before rendering to HTML. 我之前想做类似的事情,最后玩了一个代表元素的对象结构,比如服务器端DOM,可以在渲染到HTML之前在代码中进行操作。

Others have done similar things. 其他人做过类似的事情。 Notable examples are SharpDOM: http://www.codeproject.com/Articles/667581/SharpDOM-a-new-view-engine-for-ASP-NET-MVC and CityLizard: https://citylizard.codeplex.com/ 着名的例子是SharpDOM: http//www.codeproject.com/Articles/667581/SharpDOM-a-new-view-engine-for-ASP-NET-MVC和CityLizard: https ://citylizard.codeplex.com/

These days I tend to build a View Model that deals with most of the logic and manipulation of how the view should look then render that instead. 这些天我倾向于构建一个视图模型,该模型处理视图应该如何呈现的大部分逻辑和操作,然后再渲染它。

You may also find partial views (called recursively) a useful approach. 您还可以找到部分视图(称为递归),这是一种有用的方法。

If I were to go about doing this, I'd do it in a way such as this (simplified): 如果我要这样做,我会以这样的方式(简化)来做:

Item Class 物品等级

    public class HtmlListItem 
    {
         public string Content { get; set; }
         public object HtmlAttributes { get; set; }
    }

Html Helper Html助手

    public HtmlString RenderList(this HtmlHelper htmlHelper, IEnumerable<HtmlListItem> list, object uListHtmlAttributes)
        {
            TagBuilder ul = new Tagbuilder("ul");
            IDictionary<string, object> uListAttributes = new RouteValueDictionary(uListHtmlAttributes);
            ul.MergeAttributes(uListAttributes);
            StringBuilder builder = new StringBuilder();
            builder.Append(ul.ToString(TagRenderMode.StartTag) + Environment.NewLine);

            foreach (var item in list) 
            {
                TagBuilder hold = new TagBuilder("li");
                IDictionary<string, object> htmlAttributes = new RouteValueDictionary(item.HtmlAttributes);
                hold.MergeAttributes(htmlAttributes);
                hold.InnerHtml = item.Content;
                builder.Append(hold.ToString(TagRenderMode.Normal) + Environment.NewLine);
            }

            builder.Append("</ul>" + Environment.NewLine);
            return new HtmlString(builder.ToString());
        }

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

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