简体   繁体   English

无法在视图中使用我的HtmlHelper

[英]Unable to use my HtmlHelper in views

Trying to use my mvc5 project in a new asp.net vNext project, I am unable to use my HtmlHelper that automatically format a textbox. 尝试在新的asp.net vNext项目中使用我的mvc5项目时,我无法使用自动格式化文本框的HtmlHelper。

Here is my helper extension class : 这是我的辅助扩展类:

namespace MyNamespace.Helpers
{
    public static class YokoHelper
    {
        public static HtmlString YokoTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                    Expression<Func<TModel, TProperty>> expression,
                                                                    string identifiant,
                                                                    string label)
    {
        string htmlString = string.Format("<span class=\"input\">" +
                                                "{0}" +
                                                "<label class=\"input-label label-yoko\" for=\"{1}\">" +
                                                    "<span class=\"label-content label-content-yoko\">{2}</span>" +
                                                "</label>" +
                                            "</span>",
                                            htmlHelper.TextBoxFor(expression, new { @class = "input-field input-yoko", @id = identifiant }),
                                            identifiant,
                                            label);

        return new HtmlString(htmlString);
    }
}

I included a reference to my namespace in my views : 我在视图中包含了对我的命名空间的引用:

@using MyNamespace.Helpers

And try to use my helper like this : 并尝试像这样使用我的助手:

@Html.YokoTextBoxFor(m => m.Email, "email", "Email")

Any idea of what I am doing wrong ? 知道我在做什么错吗? Or why it doesn't work with vNext ? 还是为什么它不适用于vNext?

Thanks in advance 提前致谢


Edit : 编辑:

It seems that the first argument has to be an IHtmlHelper instead of HtmlHelper (MVC 6 vs MVC 5). 似乎第一个参数必须是IHtmlHelper而不是HtmlHelper(MVC 6与MVC 5)。

The modified code is in my answer bellow. 修改后的代码在下面的答案中。

Apparently in MVC6 the first argument has to be IHtmlHelper instead of HtmlHelper 显然,在MVC6中,第一个参数必须是IHtmlHelper而不是HtmlHelper

Here is the modified code : 这是修改后的代码:

    public static HtmlString YokoTextBoxFor<TModel, TProperty>(this IHtmlHelper<TModel> htmlHelper,
                                                                    Expression<Func<TModel, TProperty>> expression,
                                                                    string identifiant,
                                                                    string label)
    {
        string htmlString = string.Format("<span class=\"input\">" +
                                                "{0}" +
                                                "<label class=\"input-label label-yoko\" for=\"{1}\">" +
                                                    "<span class=\"label-content label-content-yoko\">{2}</span>" +
                                                "</label>" +
                                            "</span>",
                                            htmlHelper.TextBoxFor(expression, new { @class = "input-field input-yoko", @id = identifiant }),
                                            identifiant,
                                            label);

        return new HtmlString(htmlString);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM