2014-09-27 24 views
0

如何通過@<text></text>進行擴展方法?如何通過@<text>進行擴展方法?

我創造了這個擴展類:

public class CustomPanel 
{ 
    public CustomPanel() 
    { 
     this.Fields = new List<string>(); 
    } 

    private List<string> Fields { get; set; } 

    public void AddField(string format) 
    { 
     this.Fields.Add(format); 
    } 

    public MvcHtmlString GetHtml() 
    { 
     var sb = new StringBuilder(); 

     foreach (var field in this.Fields) 
      sb.AppendFormat("{0} <br>", field); 

     return new MvcHtmlString(sb.ToString()); 
    } 
} 

public static class PanelExtension 
{ 
    public static CustomPanel CreatePanel(this HtmlHelper html) 
    { 
     return new CustomPanel(); 
    } 
} 

當我調用該方法:

@{ 
    var panel = @Html.CreatePanel(); 
} 

panel.AddField(@<text> 
    @Html.LabelFor(model => model.BarCode) 
    @Html.CheckBoxFor(model => model.BarCode) 
</text>) 

Ocurre以下錯誤:

"<" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

我怎樣才能解決這個問題?

謝謝。

+0

你有沒有試圖改變'panel.AddField(@ ...''到panel.AddField(@(文)' – chridam 2014-09-27 16:10:21

+0

但是,我怎麼能利用@Html。on @(text)? – 2014-09-27 16:13:46

+1

爲什麼你不能做這樣簡單的事情 - '@ {var labelForHtml = Html.LabelFor(m => m.Nature); var textForHtml = Html.LabelFor(m => m .Nature); var str = string.Format(「{0} {1}」,labelForHtml,textForHtml); }'然後用str將它傳遞給你的'AddField( )'方法。 – ramiramilu 2014-09-27 16:32:13

回答

0

我用一個更好的主意解決了這個問題。

我將表達式傳遞給方法,並在裏面創建了html代碼。

public class CustomField 
{ 
    public CustomField(MvcHtmlString label, MvcHtmlString input) 
    { 
     this.Label = label; 
     this.Input = input; 
    } 

    public MvcHtmlString Label { get; private set; } 
    public MvcHtmlString Input { get; private set; } 

    public override string ToString() 
    { 
     return string.Format("{0}{1}", this.Label, this.Input); 
    } 
} 

public class CustomPanel<TModel> 
{ 
    public CustomPanel(HtmlHelper<TModel> html) 
    { 
     if (html == null) throw new ArgumentNullException("html"); 

     this.Html = html; 
     this.Fields = new List<CustomField>(); 
    } 

    private HtmlHelper<TModel> Html { get; set; } 
    private List<CustomField> Fields { get; set; } 

    public void AddField<TValue>(Expression<Func<TModel, TValue>> expression) 
    { 
     this.Fields.Add(new CustomField(this.Html.LabelFor<TModel, TValue>(expression), this.Html.TextBoxFor<TModel, TValue>(expression))); 
    } 

    public MvcHtmlString GetHtml() 
    { 
     var sb = new StringBuilder(); 
     foreach (var field in this.Fields) 
      sb.AppendFormat("{0} <br>", field); 

     return new MvcHtmlString(sb.ToString()); 
    } 
} 

public static class PanelExtension 
{ 
    public static CustomPanel<TModel> CreatePanel<TModel>(this HtmlHelper<TModel> html) 
    { 
     return new CustomPanel<TModel>(html); 
    } 
} 

查看:

@{ 
    var panel = @Html.CreatePanel(); 
    panel.AddField(model => model.Reference); 
    panel.AddField(model => model.Barcode); 
} 

@panel.GetHtml()