创建扩展方法生产公开赛和放大器;关闭标签像Html.BeginForm() [英] Create Extension Method to Produce Open & Closing Tags like Html.BeginForm()

查看:184
本文介绍了创建扩展方法生产公开赛和放大器;关闭标签像Html.BeginForm()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否可以创建具有功能和安培的扩展方法;类似于Html.BeginForm()的行为,因为它会产生一个完整的HTML标签,我可以specificy &LT内部的内容;%{&安培; }%方式> 标签

I wonder if it's possible to create an extension method which has functionality & behaviour similar to Html.BeginForm(), in that it would generate a complete Html tag, and I could specificy its contents inside <% { & } %> tags.

例如,我可以像一个观点:

For example, I could have a view like:

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

此功能会在我试图产生与<一个例子中的功能方面非常有用href=\"http://stackoverflow.com/questions/2412969/using-generics-to-create-htmlhelper-extension-methods\">this问题

This capability would be very useful in the context of the functionality I'm trying to produce with the example in this question

这会给我创造容器的类型,我会的能力。

This would give me the ability to create containers for the types that I'll be

<% 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>

<% } %>

我知道这会产生无效的XHTML,但我认为有可能是超过这一点,特别是因为这个项目不需要的XHTML验证与W3C标准的其他好处。

I realise this will produce invalid XHTML, but I think there could be other benefits that outweigh this, especially since this project doesn't require that the XHTML validate to the W3C standards.

感谢

戴夫

推荐答案

不太清楚有多少价值这个拥有超过简单地定义一个&LT; D​​IV&GT; 元素,但一些像这样

Not quite sure how much value this has over simply defining a <div> element, but something like so

/// <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;
    }
}

和使用像这样

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

将呈现

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

有无HTML属性

or without html attributes

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

这篇关于创建扩展方法生产公开赛和放大器;关闭标签像Html.BeginForm()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆