1

i am new in MVC and trying to write a sample html helper like this way here is my html helper code.

namespace MvcPractise.Extension
{
    public static class LabelHelper
    {
        public static string Label(this HtmlHelper helper, string target, string text)
        {
            return String.Format("<label for='{0}'>{1}</label>", target, text);

        }
    }
}

i use it as follows in my view like

@using MvcPractise.Extension.LabelHelper
@model MvcPractise.Models.EmployeeModel

at the top of the view i declare or refere the namespace and class name like above

and use it like @Html.Label("firstName", "First Name:")

but when i debug the code the my extension method is not getting hit. i could understand that i am doing something wrong but could not figure out. so please help. thanks

5
  • you're using @Html.Label("firstName", "First Name:") but your method is MLabel Commented Sep 16, 2013 at 9:41
  • sorry later i change that name. Commented Sep 16, 2013 at 9:41
  • tell me this line is ok in view declared at top @using MvcPractise.Extension.LabelHelper ? Commented Sep 16, 2013 at 9:42
  • Do u get .Label in intellisense? Commented Sep 16, 2013 at 10:29
  • yes but that point to in-built label() method not mine one. Commented Sep 16, 2013 at 10:43

4 Answers 4

2

If I were you, I would not name it as the original name from the Html Helper. I would try something like:

public static string CustomLabel(this HtmlHelper helper, string target, string text)
{
   return String.Format("<label for='{0}'>{1}</label>", target, text);
}

After it, just include the namespace (not the entire path to the class), for sample:

@using MvcPractise.Extension

And as the extension method, use it on Html property:

@Html.CustomLabel("firstName", "First Name:")
Sign up to request clarification or add additional context in comments.

Comments

1

@Html.Label is a built in helper, you need to try another name, like @Html.LabelEx

Rename your extension method to something, that is not in the standard library! And it should work!

Comments

-1

Try this:

  public static string Label(string target, string text)
    {
        return String.Format("<label for='{0}'>{1}</label>", target, text);

    }

1 Comment

Please try to explain your answer in at least a short sentence and try to refrain from just posting a single line of code.
-1

Try as follows it may be because of ambuiguity of Label and you are using the label of Html helper and not custom helper

@using MvcPractise.Extension

@LabelHelper.LabelCustom("firstName", "First Name:")

Your extension method:

    public static string LabelCustom(string target, string text)
    {
        return String.Format("<label for='{0}'>{1}</label>", target, text);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.