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.
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 ?
Thanks in advance
Edit :
It seems that the first argument has to be an IHtmlHelper instead of HtmlHelper (MVC 6 vs MVC 5).
The modified code is in my answer bellow.
Apparently in MVC6 the first argument has to be IHtmlHelper instead of 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);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments