2010-03-12 23 views
9

Mi chiedo se sia possibile creare un metodo di estensione con funzionalità & simile a Html.BeginForm(), in quanto genererebbe un tag Html completo e potrei specificarne il contenuto all'interno dei tag <% { & } %>.Creare un metodo di estensione per produrre tag aperti e di chiusura come Html.BeginForm()

Per esempio, ho potuto avere una vista come:

<% using(Html.BeginDiv("divId")) %> 
<% { %> 
    <!-- Form content goes here --> 
<% } %> 

Questa possibilità sarebbe molto utile nel contesto della funzionalità che sto cercando di realizzare con l'esempio in this question

Questo sarebbe dammi la possibilità di creare contenitori per i tipi che sarò

<% var myType = new MyType(123, 234); %> 
<% var tag = new TagBuilder("div"); %> 

<% using(Html.BeginDiv<MyType>(myType, tag) %> 
<% { %> 
    <!-- controls used for the configuration of MyType --> 
    <!-- represented in the context of a HTML element, e.g.: --> 

    <div class="MyType" prop1="123" prop2="234"> 
     <!-- add a select here --> 
     <!-- add a radio control here --> 
     <!-- whatever, it represents elements in the context of their type --> 
    </div> 

<% } %> 

mi rendo conto che questo produrrà XHTML valido, ma penso che ci potrebbero essere altri vantaggi che superano questo, soprattutto perché questo progetto non richiede che l'XHTML convalidi agli standard del W3C.

Grazie

Dave

risposta

14

Non del tutto sicuro di quanto valore questo ha più semplicemente definendo un elemento <div>, ma qualcosa di simile in modo

/// <summary> 
/// Represents a HTML div in an Mvc View 
/// </summary> 
public class MvcDiv : IDisposable 
{ 
    private bool _disposed; 
    private readonly ViewContext _viewContext; 
    private readonly TextWriter _writer; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="MvcDiv"/> class. 
    /// </summary> 
    /// <param name="viewContext">The view context.</param> 
    public MvcDiv(ViewContext viewContext) { 
     if (viewContext == null) { 
      throw new ArgumentNullException("viewContext"); 
     } 
     _viewContext = viewContext; 
     _writer = viewContext.Writer; 
    } 

    /// <summary> 
    /// Performs application-defined tasks associated with 
    /// freeing, releasing, or resetting unmanaged resources. 
    /// </summary> 
    public void Dispose() 
    { 
     Dispose(true /* disposing */); 
     GC.SuppressFinalize(this); 
    } 

    /// <summary> 
    /// Releases unmanaged and - optionally - managed resources 
    /// </summary> 
    /// <param name="disposing"><c>true</c> to release both 
    /// managed and unmanaged resources; <c>false</c> 
    /// to release only unmanaged resources.</param> 
    protected virtual void Dispose(bool disposing) 
    { 
     if (!_disposed) 
     { 
      _disposed = true; 
      _writer.Write("</div>"); 
     } 
    } 

    /// <summary> 
    /// Ends the div. 
    /// </summary> 
    public void EndDiv() 
    { 
     Dispose(true); 
    } 
} 


/// <summary> 
/// HtmlHelper Extension methods for building a div 
/// </summary> 
public static class DivExtensions 
{ 
    /// <summary> 
    /// Begins the div. 
    /// </summary> 
    /// <param name="htmlHelper">The HTML helper.</param> 
    /// <returns></returns> 
    public static MvcDiv BeginDiv(this HtmlHelper htmlHelper) 
    { 
     // generates <div> ... </div>> 
     return DivHelper(htmlHelper, null); 
    } 

    /// <summary> 
    /// Begins the div. 
    /// </summary> 
    /// <param name="htmlHelper">The HTML helper.</param> 
    /// <param name="htmlAttributes">The HTML attributes.</param> 
    /// <returns></returns> 
    public static MvcDiv BeginDiv(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes) 
    { 
     // generates <div> ... </div>> 
     return DivHelper(htmlHelper, htmlAttributes); 
    } 

    /// <summary> 
    /// Ends the div. 
    /// </summary> 
    /// <param name="htmlHelper">The HTML helper.</param> 
    public static void EndDiv(this HtmlHelper htmlHelper) 
    { 
     htmlHelper.ViewContext.Writer.Write("</div>"); 
    } 

    /// <summary> 
    /// Helps build a html div element 
    /// </summary> 
    /// <param name="htmlHelper">The HTML helper.</param> 
    /// <param name="htmlAttributes">The HTML attributes.</param> 
    /// <returns></returns> 
    private static MvcDiv DivHelper(this HtmlHelper htmlHelper, IDictionary<string, object> 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; 
    } 
} 

e utilizzare in questo modo

<% using (Html.BeginDiv(new Dictionary<string, object>{{"class","stripey"}})) 
{ %> 
     <p>Content Here</p> 
<% } %> 

renderizza

<div class="stripey"> 
    <p>Content Here</p> 
</div> 

o senza attributi HTML

<% using (Html.BeginDiv()) 
{ %> 
     <p>Content Here</p> 
<% } %> 
+0

grazie, mi mancava la cosa viewcontext.writer! – koenmetsu

Problemi correlati