2011-08-01 8 views
1

私は自分のBeginLabelヘルパーをMvcのために動かす必要があります。私はhtml.beginForm/ajax.beginFormメソッドのMvcソースからコンセプトをチェック/盗んだ。実装.net mvc BeginLabel BeginForm response.writeのような問題

protected virtual void Dispose(bool disposing) 
{ 
    if (!_disposed) 
    { 
     _disposed = true; 
     _httpResponse.Write("</label>"); 
    } 
} 

使い方は次のようになります:

public static Label BeginLabel(this HtmlHelper htmlHelper) 
{ 
    TagBuilder tagBuilder = new TagBuilder("label"); 
    HttpResponseBase response = htmlHelper.ViewContext.HttpContext.Response; 
    response.Write(tagBuilder.ToString(TagRenderMode.StartTag)); 
    return new Label(response); 
} 

ラベルは、単純にラベルを閉鎖可能にするためのIDisposableインターフェイスを実装して、私はのように何かが欠けてるよう

@using (Html.BeginLabel()) 
{ 
    @Html.TextBoxFor(f => f.FirstName) 
    @Html.ValidationMessageFor(f => f.FirstName) 
} 

に見えますラベルは常にhtmlの先頭に表示されますが、これは私には明らかですが、私はレスポンスに書いているので、ネイティブBegin Form()はこれを達成しています。誰もこれにどのような光を当てることができますか?私は他の人を助けることができる

+0

これはhttp://stackoverflow.com/questions/2435898/create-extension-でカバーされているように見えますmethod-to-produce-open-closing-tags-like-html-beginform –

+0

このような小さな利益のために非常に多くの仕事のようです。プラス、私はあなたがそれから何を得るか分からない。テキストなしのラベル?また、意味的に言えば、ラベル内に検証メッセージを入れるかどうかはわかりません。 –

答えて

2
public class MvcLabel : IDisposable 
{ 
    // Fields 
    private bool _disposed; 
    private readonly TextWriter _writer; 

    public MvcLabel(ViewContext viewContext) 
    { 
     if (viewContext == null) 
     { 
      throw new ArgumentNullException("viewContext"); 
     } 
     this._writer = viewContext.Writer; 
    } 

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

    protected virtual void Dispose(bool disposing) 
    { 
     if (!this._disposed) 
     { 
      this._disposed = true; 
      this._writer.Write("</label>"); 
     } 
    } 

    public void EndLabel() 
    { 
     this.Dispose(true); 
    } 
} 

public static class HtmlHelperExtension 
{ 
    // Methods 
    public static MvcLabel BeginLabel(this HtmlHelper html, string expression) 
    { 
     return html.BeginLabel(expression, null); 
    } 

    public static MvcLabel BeginLabel(this HtmlHelper html, string expression, string labelText) 
    { 
     return LabelHelper(html, ModelMetadata.FromStringExpression(expression, html.ViewData), expression, labelText); 
    } 

    public static MvcLabel BeginLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) 
    { 
     return html.BeginLabelFor<TModel, TValue>(expression, null); 
    } 

    public static MvcLabel BeginLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText) 
    { 
     return LabelHelper(html, ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), labelText); 
    } 

    public static MvcLabel BeginLabelForModel(this HtmlHelper html) 
    { 
     return html.BeginLabelForModel(null); 
    } 

    public static MvcLabel BeginLabelForModel(this HtmlHelper html, string labelText) 
    { 
     return LabelHelper(html, html.ViewData.ModelMetadata, string.Empty, labelText); 
    } 

    public static void EndLabel(this HtmlHelper htmlHelper) 
    { 
     htmlHelper.ViewContext.Writer.Write("</label>"); 
    } 

    internal static MvcLabel LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null) 
    { 
     string str = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>())); 

     TagBuilder tagBuilder = new TagBuilder("label"); 
     tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); 

     html.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); 

     if (!string.IsNullOrEmpty(str)) 
     { 
      tagBuilder = new TagBuilder("span"); 
      tagBuilder.SetInnerText(str); 
      html.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.Normal)); 
     } 

     return new MvcLabel(html.ViewContext); 
    } 
} 

希望...

+0

これは混乱しています。著者は、ラベルタグに "for"属性を使用しない要素を含める機能を利用したいと考えています。なぜそこにスパンを入れているのかは分かりません。 –

+0

ええ、これは大丈夫なことではありませんが、正しいと多くの変更を行うことはありません。 – DanH

+0

また、人々が管理されていないリソースを使用して2段階の廃棄パターンを使用する理由についても私は困惑しています。これはMSのコードでも同じです。その永遠の永遠の神話のように! – DanH