无法在视图中使用我的 HtmlHelper
Unable to use my HtmlHelper in views
尝试在新的 asp.net vNext 项目中使用我的 mvc5 项目时,我无法使用自动格式化文本框的 HtmlHelper。
这是我的助手扩展 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);
}
}
我在我的视图中包含了对我的命名空间的引用:
@using MyNamespace.Helpers
并尝试像这样使用我的助手:
@Html.YokoTextBoxFor(m => m.Email, "email", "Email")
知道我做错了什么吗?或者为什么它不适用于 vNext?
提前致谢
编辑:
第一个参数似乎必须是 IHtmlHelper 而不是 HtmlHelper(MVC 6 与 MVC 5)。
修改后的代码在我下面的回答中。
显然在 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);
}
尝试在新的 asp.net vNext 项目中使用我的 mvc5 项目时,我无法使用自动格式化文本框的 HtmlHelper。
这是我的助手扩展 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);
}
}
我在我的视图中包含了对我的命名空间的引用:
@using MyNamespace.Helpers
并尝试像这样使用我的助手:
@Html.YokoTextBoxFor(m => m.Email, "email", "Email")
知道我做错了什么吗?或者为什么它不适用于 vNext?
提前致谢
编辑:
第一个参数似乎必须是 IHtmlHelper 而不是 HtmlHelper(MVC 6 与 MVC 5)。
修改后的代码在我下面的回答中。
显然在 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);
}