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
Asp.net mvc Razor:无法在@Section中呈现Html.Label帮助程序(仅输出源)_Asp.net Mvc_Razor_Rendering_Asp.net Mvc Migration - Fatal编程技术网

Asp.net mvc Razor:无法在@Section中呈现Html.Label帮助程序(仅输出源)

Asp.net mvc Razor:无法在@Section中呈现Html.Label帮助程序(仅输出源),asp.net-mvc,razor,rendering,asp.net-mvc-migration,Asp.net Mvc,Razor,Rendering,Asp.net Mvc Migration,我不知道如何在Html.LabelFor中使用@class,所以我用在so上找到的代码更新了Html.Label的扩展助手 我的LabelExtension.cs文件具有以下类: public static class LabelExtensions { public static string Label(this HtmlHelper helper, string labelFor,

我不知道如何在Html.LabelFor中使用@class,所以我用在so上找到的代码更新了Html.Label的扩展助手

我的LabelExtension.cs文件具有以下类:

 public static class LabelExtensions
    {
        public static string Label(this HtmlHelper helper,
                                string labelFor,
                                string value,
                                object htmlAttributes)
        {
            TagBuilder labelBuilder = new TagBuilder("label");
            if (!string.IsNullOrEmpty(labelFor))
            {
                labelBuilder.Attributes.Add("for", labelFor);
            }
            labelBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            labelBuilder.SetInnerText(value);
            return labelBuilder.ToString(TagRenderMode.Normal);
        } 
    }
我最初在MVC2.aspx页面中使用了以下内容(我正在将其转换为.cshtml):

这与@section有关吗?还是我正在尝试使用MVC2的扩展

提前感谢您的任何想法/建议

编辑:这是我在评论中引用的代码,适用于我的情况。谢谢你的帮助

public static MvcHtmlString Label(this HtmlHelper htmlHelper, string labelFor, string value,
                                      object htmlAttributes)
    {
        var tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tagBuilder.MergeAttribute("for", labelFor.Replace(".", tagBuilder.IdAttributeDotReplacement), true);
        tagBuilder.SetInnerText(value);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
    }

这是因为您的助手正在返回默认编码的字符串。您的助手应该返回MvcHtmlString并将return语句更改为

 return new MvcHtmlString(labelBuilder.ToString(TagRenderMode.Normal));

这让我走上了正确的道路。我的标签现在按照CSS类的预期呈现。有关在我的特定情况下工作的代码,请参见上面的编辑。我在.cs文件中编写代码的方式表明,MvcHtmlString不包含接受1个参数的构造函数
<label class="mylabelstyle" for="txt_name_udq">Your First Name (required):</label>;
&lt;label class=&quot;mylabelstyle&quot; for=&quot;txt_name_udq&quot;&gt;Your First Name (required):&lt;/label&gt;;
public static MvcHtmlString Label(this HtmlHelper htmlHelper, string labelFor, string value,
                                      object htmlAttributes)
    {
        var tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tagBuilder.MergeAttribute("for", labelFor.Replace(".", tagBuilder.IdAttributeDotReplacement), true);
        tagBuilder.SetInnerText(value);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
    }
 return new MvcHtmlString(labelBuilder.ToString(TagRenderMode.Normal));