Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not quite sure how much value this has over simply defining a <code>&lt;div&gt;</code> element, but something like so</p> <pre><code>/// &lt;summary&gt; /// Represents a HTML div in an Mvc View /// &lt;/summary&gt; public class MvcDiv : IDisposable { private bool _disposed; private readonly ViewContext _viewContext; private readonly TextWriter _writer; /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="MvcDiv"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="viewContext"&gt;The view context.&lt;/param&gt; public MvcDiv(ViewContext viewContext) { if (viewContext == null) { throw new ArgumentNullException("viewContext"); } _viewContext = viewContext; _writer = viewContext.Writer; } /// &lt;summary&gt; /// Performs application-defined tasks associated with /// freeing, releasing, or resetting unmanaged resources. /// &lt;/summary&gt; public void Dispose() { Dispose(true /* disposing */); GC.SuppressFinalize(this); } /// &lt;summary&gt; /// Releases unmanaged and - optionally - managed resources /// &lt;/summary&gt; /// &lt;param name="disposing"&gt;&lt;c&gt;true&lt;/c&gt; to release both /// managed and unmanaged resources; &lt;c&gt;false&lt;/c&gt; /// to release only unmanaged resources.&lt;/param&gt; protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposed = true; _writer.Write("&lt;/div&gt;"); } } /// &lt;summary&gt; /// Ends the div. /// &lt;/summary&gt; public void EndDiv() { Dispose(true); } } /// &lt;summary&gt; /// HtmlHelper Extension methods for building a div /// &lt;/summary&gt; public static class DivExtensions { /// &lt;summary&gt; /// Begins the div. /// &lt;/summary&gt; /// &lt;param name="htmlHelper"&gt;The HTML helper.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MvcDiv BeginDiv(this HtmlHelper htmlHelper) { // generates &lt;div&gt; ... &lt;/div&gt;&gt; return DivHelper(htmlHelper, null); } /// &lt;summary&gt; /// Begins the div. /// &lt;/summary&gt; /// &lt;param name="htmlHelper"&gt;The HTML helper.&lt;/param&gt; /// &lt;param name="htmlAttributes"&gt;The HTML attributes.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MvcDiv BeginDiv(this HtmlHelper htmlHelper, IDictionary&lt;string, object&gt; htmlAttributes) { // generates &lt;div&gt; ... &lt;/div&gt;&gt; return DivHelper(htmlHelper, htmlAttributes); } /// &lt;summary&gt; /// Ends the div. /// &lt;/summary&gt; /// &lt;param name="htmlHelper"&gt;The HTML helper.&lt;/param&gt; public static void EndDiv(this HtmlHelper htmlHelper) { htmlHelper.ViewContext.Writer.Write("&lt;/div&gt;"); } /// &lt;summary&gt; /// Helps build a html div element /// &lt;/summary&gt; /// &lt;param name="htmlHelper"&gt;The HTML helper.&lt;/param&gt; /// &lt;param name="htmlAttributes"&gt;The HTML attributes.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private static MvcDiv DivHelper(this HtmlHelper htmlHelper, IDictionary&lt;string, object&gt; htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("div"); tagBuilder.MergeAttributes(htmlAttributes); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); MvcDiv div = new MvcDiv(htmlHelper.ViewContext); return div; } } </code></pre> <p>and use like so</p> <pre><code>&lt;% using (Html.BeginDiv(new Dictionary&lt;string, object&gt;{{"class","stripey"}})) { %&gt; &lt;p&gt;Content Here&lt;/p&gt; &lt;% } %&gt; </code></pre> <p>will render</p> <pre><code>&lt;div class="stripey"&gt; &lt;p&gt;Content Here&lt;/p&gt; &lt;/div&gt; </code></pre> <p>or without html attributes</p> <pre><code>&lt;% using (Html.BeginDiv()) { %&gt; &lt;p&gt;Content Here&lt;/p&gt; &lt;% } %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload