2015-04-30 23 views
2

试图在新的asp.net vNext项目中使用我的mvc5项目时,我无法使用自动格式化文本框的HtmlHelper。无法在视图中使用我的HtmlHelper

这是帮助我扩展类:

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); 
    } 
} 

我包括我的意见,我的命名空间的引用:

@using MyNamespace.Helpers 

,并尝试使用我的帮助是这样的:

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

任何想法我做错了什么?或者为什么它不适用于vNext?

预先感谢


编辑:

看来,第一个参数必须是一个IHtmlHelper代替的HtmlHelper(MVC 6 VS MVC 5)。

修改后的代码在我的回答下面。

+0

您的帮手命名空间是“MyNamespace.Helpers”您正在使用的“OuAllonsNous .Helpers“命名空间在您的视图中。 – Charly

+0

没有抱歉,我重命名了我的帖子命名空间。 –

+0

我回答了我自己的问题,因为我找到了解决方案。谢谢@RogerWilliam您的评论 –

回答

4

显然,在MVC6的第一个参数必须是IHtmlHelper代替的HtmlHelper

下面是修改代码:

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); 
    } 
+0

哈哈只是想回答你 –

相关问题