ASP.NET MVC HTML帮助器 [英] ASP.NET MVC Html Helper

查看:82
本文介绍了ASP.NET MVC HTML帮助器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一些Html Helper,它们将具有一个开始标记和一个结束标记,其中将包含其他内容,例如Html.BeginForm. 例如,在Razor中,我们可以使用Html.BeginForm帮助器,其语法如下:

I try to create some Html Helpers which will have an opening tag and closing tag which will include other contents like the Html.BeginForm does. For example in Razor we can use the Html.BeginForm helper which has the following syntax:

    @using (Html.BeginForm())
    {
    }

此代码将在和中包含大括号的内容.我解决用内容打开和关闭标签的唯一方法是使用两个html帮助器.我定义了两个html助手:

This code will include the contents of curly brackets within a and . The only way that I solved opening and closing a tag with contents is by using two html helpers. I define two html helpers:

    public static MvcHtmlString StartForm(this System.Web.Mvc.HtmlHelper helper)
    {
        return new MvcHtmlString("<form>");
    }
    public static MvcHtmlString EndForm(this System.Web.Mvc.HtmlHelper helper)
    {
        return new MvcHtmlString("</form>");
    }

然后我通过以下示例使用助手:

Then I use the helpers using the following example:

    @Html.StartForm()
    contents
    @Html.EndForm()

但是我希望能够制作一个HTML帮助器,该帮助器在视图中具有以下格式:

But I would like to be able to make one html helper which will have the following format in the view:

    @using (Html.MyForm())
    {
            <text>contents</text>
    }

有人可以帮助我解决这个问题,因为我什至不知道如何搜索它.

Can someone help me with this problem because I do not know even how to search it.

推荐答案

您可以定义类,就像实现MvcForm的方式一样.下面的类允许您创建一个包含其他元素的标签.

You can define a class just like the way the MvcForm is implemented. The class below allows you to create a tag which contains other elements.

public class MvcTag : IDisposable
{
    private string _tag;
    private bool _disposed;
    private readonly FormContext _originalFormContext;
    private readonly ViewContext _viewContext;
    private readonly TextWriter _writer;

    public MvcTag(ViewContext viewContext, string tag)
    {
        if (viewContext == null)
        {
            throw new ArgumentNullException("viewContext");
        }

        _viewContext = viewContext;
        _writer = viewContext.Writer;
        _originalFormContext = viewContext.FormContext;
        viewContext.FormContext = new FormContext();
        _tag = tag;
        Begin(); // opening the tag
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void Begin()
    {
        _writer.Write("<" + _tag + ">");
    }

    private void End()
    {
        _writer.Write("</" + _tag + ">");
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            _disposed = true;
            End(); // Closing the tag

            if (_viewContext != null)
            {
                _viewContext.OutputClientValidation();
                _viewContext.FormContext = _originalFormContext;
            }
        }
    }

    public void EndForm()
    {
        Dispose(true);
    }
}

要以MvcForm的方式使用此MvcTag,我们必须定义一个扩展名

To make use of this MvcTag in the way the MvcForm is used, we have to define an extension

public static class HtmlHelperExtensions
{
    public static MvcTag BeginTag(this HtmlHelper htmlHelper, string tag)
    {
        return new MvcTag(htmlHelper.ViewContext, tag);
    }
}

就是这样.现在,您可以将其用作:

And that's it. Now you can use it as:

@using(Html.BeginTag("div")) @* This creates a <div>, alternatively, you can create any tag with it ("span", "p" etc.) *@
{ 
    <p>Contents</p>
}

这篇关于ASP.NET MVC HTML帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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