我自己的滚动@ Html.Beginform() [英] rolling my own @Html.Beginform()

查看:199
本文介绍了我自己的滚动@ Html.Beginform()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个自定义的验证组将显示在一个div所有缺少的元素。我希望能够使用,将写出该div自定义@ Html.BeginForm()方法,但我真的不知道从哪里开始,甚至因为这螺母有点艰难不仅仅是一个HTML扩展破解了写出一个标签或字符串(格式封装的数据/控制,并通过在一端封闭})。

我看着建于beginform()方法的元数据版本,这是不是对我有很大帮助。从本质上讲,我只是想延长,如果该方法可能,并将它写出MvcHtmlString的div,这将是显示/ JavaScript中隐藏的。

最终在那里我挣扎是搞清楚如何编写具有初这个自定义帮助和结束部分吧。

  @using(Html.BeginForm())
{
...}

我希望能够做这样的事:

  @using(Html.VBeginForm())
{
...}

和有我的渲染HTML额外

编辑:从建议增加code以下

 公共类VBeginForm:IDisposable接口
{
    私人只读的HtmlHelper _helper;
    公共VBeginForm(的HtmlHelper的HtmlHelper,串AREANAME)
    {
        _helper =的HtmlHelper;
        VAR集装箱=​​新TagBuilder(形式);
        container.GenerateId(AREANAME);
        VAR作家= _helper.ViewContext.Writer;
        writer.Write(container.ToString(TagRenderMode.StartTag));
    }    公共无效的Dispose()
    {
        _helper.ViewContext.Writer.Write(< /形式为GT;);
    }
}


解决方案

您需要编写打印到的HtmlHelper 类的扩展方法helper.ViewContext.Writer

该方法应该返回一个的IDisposable 即会在其的Dispose 方法关闭标签。

I am writing a custom validation set that will display all missing elements on a div. I'd like to be able to use a custom @Html.BeginForm() method that will write out that div but I'm really not sure where to even begin as this nut is a little tougher to crack than just a html extension that writes out a Tag or String (the form encapsulates data/controls and is closed by } at the end).

I looked at the metadata version of the built in beginform() method and it wasn't much help to me. Essentially, I just want to extend that method if possible and have it write out a MvcHtmlString of a div that will be show/hidden from javascript.

ultimately where I'm struggling is figuring out how to write this custom helper that has the beginning and ending component to it.

@using(Html.BeginForm())
{
...

}

I'd want to be able to do something like this:

@using(Html.VBeginForm())
{
...

}

and have that render my extra html

EDIT: adding code from suggestion below

public class VBeginForm : IDisposable
{
    private readonly HtmlHelper _helper;
    public VBeginForm(HtmlHelper htmlHelper, string areaName)
    {
        _helper = htmlHelper;
        var container = new TagBuilder("form");
        container.GenerateId(areaName);
        var writer = _helper.ViewContext.Writer;
        writer.Write(container.ToString(TagRenderMode.StartTag));
    }

    public void Dispose()
    {
        _helper.ViewContext.Writer.Write("</form>");
    }
}

解决方案

You need to write an extension method for the HtmlHelper class that prints to helper.ViewContext.Writer.

The method should return an IDisposable that prints the closing tag in its Dispose method.

这篇关于我自己的滚动@ Html.Beginform()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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