Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能通过@<;文本>;对于扩展方法?_C#_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# 我怎样才能通过@<;文本>;对于扩展方法?

C# 我怎样才能通过@<;文本>;对于扩展方法?,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,如何传递扩展方法的@ 我创建了这个扩展类: public class CustomPanel { public CustomPanel() { this.Fields = new List<string>(); } private List<string> Fields { get; set; } public void AddField(string format) { this.Fie

如何传递扩展方法的
@

我创建了这个扩展类:

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();
    }
}
公共类自定义面板
{
公众谘询委员会(
{
this.Fields=新列表();
}
私有列表字段{get;set;}
公共void AddField(字符串格式)
{
此.Fields.Add(格式);
}
公共MvcHtmlString GetHtml()
{
var sb=新的StringBuilder();
foreach(此.Fields中的var字段)
sb.AppendFormat(“{0}
”,字段); 返回新的MvcHtmlString(sb.ToString()); } } 公共静态类扩展 { 公共静态CustomPanel CreatePanel(此HtmlHelper html) { 返回新的CustomPanel(); } }
当我调用该方法时:

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

panel.AddField(@<text>
    @Html.LabelFor(model => model.BarCode)
    @Html.CheckBoxFor(model => model.BarCode)
</text>)
@{
var panel=@Html.CreatePanel();
}
panel.AddField(@
@LabelFor(model=>model.BarCode)
@CheckBoxFor(model=>model.BarCode)
)
出现以下错误:


“我用一个更好的主意解决了这个问题

我将表达式传递给方法,并在其中创建了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);
    }
}

您是否尝试过将
panel.AddField(@…
更改为
panel.AddField(@(text)
?但是,我如何使用@Html.on@(text)?您为什么不能做这样简单的事情-
{var labelForHtml=Html.LabelFor(m=>m.Nature);var textForHtml=Html.LabelFor(m=>m.Nature);var str=string.Format({0}{1},labelForHtml,textForHtml);}
然后使用str将其传递给
AddField()
方法。
@{
    var panel = @Html.CreatePanel();
    panel.AddField(model => model.Reference);
    panel.AddField(model => model.Barcode);
}

@panel.GetHtml()