ASP.NET MVC HtmlHelper扩展方法只有在有内容的情况下才能包装

时间:2014-09-08 07:52:48

标签: asp.net-mvc html-helper

我正在制作一个可配置的表单 - 管理员选择显示哪些输入。输入由字段集包装。

我想让HtmlHelper生成字段集,只要它有内容 - 至少有一个输入字段来防止这种情况

<fieldset id="Name">
   <legend>Name</legend>
   <input type="text" placeholer="Forename"></input>
   <input type="text" placeholer="Surname"></input>
</fieldset>


<fieldset id="Address">
   <legend>Address</legend>
</fieldset>

我们有空的字段集,就像地址一样。

我制作了FieldSetHelper

public class FieldSetHelper: IDisposable
{
    private readonly HtmlHelper _htmlHelper;
    private readonly string _fieldSetId;
    private readonly string _legendId;
    private readonly string _legendText;

    public FieldSetHelper(HtmlHelper htmlHelper, string fieldSetId, string legendId, string legendText)
    {
        _htmlHelper = htmlHelper;
        _fieldSetId = fieldSetId;
        _legendId = legendId;
        _legendText = legendText;

        _htmlHelper.ViewContext.Writer = new StringWriter();

        OpenFieldSet();
        AddLegend();
    }

    private void OpenFieldSet()
    {
        string id = string.IsNullOrWhiteSpace(_fieldSetId) ? string.Empty : string.Format(" id=\"{0}\"", _fieldSetId);
        _htmlHelper.ViewContext.Writer.WriteLine(string.Format("<fieldset" + id + ">"));
    }

    private void CloseFieldSet()
    {
        _htmlHelper.ViewContext.Writer.WriteLine(string.Format("</fieldset>"));
    }

    private void AddLegend()
    {
        string id = string.IsNullOrWhiteSpace(_legendId) ? string.Empty : string.Format(" id=\"{0}\"", _legendId);
        _htmlHelper.ViewContext.Writer.WriteLine("<legend"+id+">");
        _htmlHelper.ViewContext.Writer.WriteLine(_legendText);
        _htmlHelper.ViewContext.Writer.WriteLine("</legend>");
    }

    public void Dispose()
    {
        CloseFieldSet();
    }

但如果内容为空,我不知道如何不生成它。 以下是视图的一部分:

using (Html.BeginFieldSet("Address", null, "Address"))
{
   @Html.EditorFor(m => m.Address, new {Model.VisibleInputFields})
}

1 个答案:

答案 0 :(得分:0)

我最近有过类似的情况。如果外部元素(使用元素生成的元素)没有任何项目,我试图添加一些“No Items Here”文本。这是我最终做的未经测试的修改版本,但如果你仍然在寻找答案,它应该让你接近。

 public class FieldSetHelper: IDisposable
{
    private readonly HtmlHelper _htmlHelper;
    private readonly string _fieldSetId;
    private readonly string _legendId;
    private readonly string _legendText;

    public FieldSetHelper(HtmlHelper htmlHelper, string fieldSetId, string legendId, string legendText)
    {
        _htmlHelper = htmlHelper;
        _fieldSetId = fieldSetId;
        _legendId = legendId;
        _legendText = legendText;

        _htmlHelper.ViewContext.Writer = new StringWriter();

        OpenFieldSet();
        AddLegend();
    }


    private string BuildOpenFieldSetString(){
        string id = string.IsNullOrWhiteSpace(_fieldSetId) ? string.Empty : string.Format(" id=\"{0}\"", _fieldSetId);
        return string.Format("<fieldset" + id + ">");
    }

    private void OpenFieldSet()
    {
        _htmlHelper.ViewContext.Writer.WriteLine(BuildOpenFieldSetString());
    }

    private void CloseFieldSet()
    {
        _htmlHelper.ViewContext.Writer.WriteLine(string.Format("</fieldset>"));
    }

    private void AddLegend()
    {
        string id = string.IsNullOrWhiteSpace(_legendId) ? string.Empty : string.Format(" id=\"{0}\"", _legendId);
        _htmlHelper.ViewContext.Writer.WriteLine("<legend"+id+">");
        _htmlHelper.ViewContext.Writer.WriteLine(_legendText);
        _htmlHelper.ViewContext.Writer.WriteLine("</legend>");
    }

    public void Dispose()
    {
        var htmlString = _htmlHelper.ViewContext.Writer.ToString();
        var index = htmlString.Trim().LastIndexOf(BuildOpenFieldSetString().Trim());
        if(html.IndexOf("<input", index) == -1){
            _htmlHelper.ViewContext.Writer = new StringWriter();
            _htmlHelper.ViewContext.Writer.WriteLine(html.SubString(0,index));
        }else{
            CloseFieldSet();
        }
    }