Unable to use my HtmlHelper in views

Béranger

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.

Béranger

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use HtmlHelper extensions in helper views

From Dev

LANSA - unable to use views and joins

From Dev

Unable to use recycler views in multiple tabs

From Dev

Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

From Dev

Unable to use bluetooth on my device

From Dev

How to display @Html htmlhelper and @using in same line in MVC views?

From Dev

Do I need to use request.is_ajax() in my views?

From Dev

How to use nested views that correspond to my nested viewmodels?

From Dev

Use an Anonymous object in HtmlHelper extension method

From Dev

CakePHP - Use HtmlHelper::link with URL title

From Dev

Unable to use reactive element in my shiny app

From Dev

I am Unable to use my own annotation

From Dev

Unable to use my DRF serializers manually

From Dev

Unable to use my DRF serializers manually

From Dev

Unable to use putExtra( ) and getExtras( ) for my Android App

From Dev

Unable to use reactive element in my shiny app

From Dev

Unable to use FQL with my Facebook application

From Dev

Unable to use the same relative path in my program AND my unit tests

From Dev

Unable to add views to a scrollView

From Dev

Django unable to split views

From Dev

Making my own HtmlHelper extension for input that works with model binding

From Dev

pass values from other properties on my model to custom htmlhelper

From Dev

RenderBody not rendering my views

From Dev

Error with my views and subviews

From Dev

RenderBody not rendering my views

From Dev

Unable to use abstract state to provide initial shared views in Angular UI-router

From Dev

Unable to use strongly-typed views when using PagedList.MVC

From Dev

Unable to use abstract state to provide initial shared views in Angular UI-router

From Dev

Unable to cast the object type "System.Web.Mvc.HtmlHelper` 1 [System.Object] "to type" System.Web.Mvc.HtmlHelper "

Related Related

  1. 1

    How to use HtmlHelper extensions in helper views

  2. 2

    LANSA - unable to use views and joins

  3. 3

    Unable to use recycler views in multiple tabs

  4. 4

    Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

  5. 5

    Unable to use bluetooth on my device

  6. 6

    How to display @Html htmlhelper and @using in same line in MVC views?

  7. 7

    Do I need to use request.is_ajax() in my views?

  8. 8

    How to use nested views that correspond to my nested viewmodels?

  9. 9

    Use an Anonymous object in HtmlHelper extension method

  10. 10

    CakePHP - Use HtmlHelper::link with URL title

  11. 11

    Unable to use reactive element in my shiny app

  12. 12

    I am Unable to use my own annotation

  13. 13

    Unable to use my DRF serializers manually

  14. 14

    Unable to use my DRF serializers manually

  15. 15

    Unable to use putExtra( ) and getExtras( ) for my Android App

  16. 16

    Unable to use reactive element in my shiny app

  17. 17

    Unable to use FQL with my Facebook application

  18. 18

    Unable to use the same relative path in my program AND my unit tests

  19. 19

    Unable to add views to a scrollView

  20. 20

    Django unable to split views

  21. 21

    Making my own HtmlHelper extension for input that works with model binding

  22. 22

    pass values from other properties on my model to custom htmlhelper

  23. 23

    RenderBody not rendering my views

  24. 24

    Error with my views and subviews

  25. 25

    RenderBody not rendering my views

  26. 26

    Unable to use abstract state to provide initial shared views in Angular UI-router

  27. 27

    Unable to use strongly-typed views when using PagedList.MVC

  28. 28

    Unable to use abstract state to provide initial shared views in Angular UI-router

  29. 29

    Unable to cast the object type "System.Web.Mvc.HtmlHelper` 1 [System.Object] "to type" System.Web.Mvc.HtmlHelper "

HotTag

Archive