Create HTML.Helper

Last post 09-09-2008 12:47 PM by PaulBlamire. 9 replies.

Sort Posts:

  • Create HTML.Helper

    09-08-2008, 1:25 PM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    Hello,

    I would like to create a new Html.Helper that renders as follows:

    <div>Solid message box<div></div></div>
     
    All attributes should be added to the outside div. As input I only want to send the Message Text.
     
    Could someone, please, tell me how to do this?
     
    Thanks,
    Miguel
  • Re: Create HTML.Helper

    09-08-2008, 2:05 PM
    Answer
    • Member
      746 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 147

    namespace MyNameSpace

    public static class HtmlHelperExtensions
    {
       public static string SolidMsgBox(this System.Web.Mvc.HtmlHelper htmlHelper, string text) 
       {
         return string.Format("<div class="Whatever"><div>{0}</div></div>", text);
       }
    }
    }


    usage

    Note make sure you reference MyNameSpace in your view to be able to see your new extension method

     <%= Html.SolidMsgBox("Hello") %>

     Is that what you mean?

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
  • Re: Create HTML.Helper

    09-08-2008, 3:22 PM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    Yes,

    but can I use: <%= Html.SolidMsgBox("Hello", "", new { @class = "Message" }) %>

    Where the attributes are added to the outside Div, i.e:

    <div class="Message"><div>{0}</div></div>

    Thanks,

    Miguel

     

     

  • Re: Create HTML.Helper

    09-08-2008, 3:43 PM
    Answer
    • Member
      746 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 147

    Ok I understand now. This should do what you want. 

    public
    static string SolidMsgBox(this System.Web.Mvc.HtmlHelper htmlHelper, string text)
    {
    return SolidMsgBox(htmlHelper, text, null);
    }

    public static string SolidMsgBox(this System.Web.Mvc.HtmlHelper htmlHelper, string text, object attributes)
    {
    IDictionary<string, object> attributesDict = (IDictionary<string, object>)new RouteValueDictionary(attributes);
    System.Web.Mvc.
    TagBuilder builder = new System.Web.Mvc.TagBuilder("div");
    builder.MergeAttributes<
    string, object>(attributesDict);
    builder.InnerHtml =
    string.Format("<div>{0}</div>", text);
    return builder.ToString();
    }

     

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
  • Re: Create HTML.Helper

    09-08-2008, 6:09 PM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    That's exactly what I was looking for. Thank you.

    I am having only one problem. I created the file SolidMsgBox.cs and added the code there:

    public static class HtmlHelperExtensions {
      public static string SolidMsgBox(this HtmlHelper helper, string name) {
        return SolidMsgBox(helper, name, null, null);
      }
      ' ...
    }

    Then I was creating a new .cs file for a new helper the same as this one and I got the following:

    The namespace '<global namespace>' already contains a definition for 'HtmlHelperExtensions'

    Does this mean that I need to place all my Helper extensions in the same file? Can't I place them in different files?

    Thanks,

    Miguel

  • Re: Create HTML.Helper

    09-09-2008, 3:46 AM
    • Member
      746 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 147

    No, problem. You can place your html extensions in as many different class names as you like. The important things are:

    1) The class is static

    2) The extension method is static

    3) The extension method uses the this keyword on the first parameter of type of HtmlHelper

    4) You reference the classes namespace from your view.

    Please accept as answer if you feel like I covered everything.

     Thanks, Paul

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
  • Re: Create HTML.Helper

    09-09-2008, 10:44 AM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

     Paul,

     what I meant was instead of having the ToolTip inside a new class named HtmlHelperExtentions can I make my ToolTip method to some how extend the HtmlHelper, i.e., to be also inside "HtmlHelper'?

      public static class HtmlHelperExtensions {

        public static string ToolTip(this HtmlHelper helper, string name) {
          ...
        }
      ...

    Would become:

       public static class HtmlHelper {

        public static string ToolTip(string name) {
          ...
        }
      ...

    Under System.Web.MVC.

    And if yes, should I do this?

    Thanks,
    Miguel

  • Re: Create HTML.Helper

    09-09-2008, 12:08 PM
    • Member
      746 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 147

    You can do that as the team have implemented HtmlHelper as a partial class. The code would look like: 

    namespace System.Web.Mvc
    {
        public partial class HtmlHelper
        {
            public string ToolTip(this HtmlHelper helper, string name)
            {
                 ....Code
            }
        }
    }

    The reason that I implemented it as an extension method is that I think it is a dubious practice to implement your own custom code inside a Microsoft namespace.

    The choice is yours though

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
  • Re: Create HTML.Helper

    09-09-2008, 12:35 PM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947

    PaulBlamire:

    The reason that I implemented it as an extension method is that I think it is a dubious practice to implement your own custom code inside a Microsoft namespace.

    The choice is yours though

    But if I am extending a Helper, why not? Then they will all be in the same place ...

    The same happens if I am extending some functionality in Generics like the PagedList ...

    Should I add all my code into my own namespace?

    I have seen different approaches and I am in the moment trying to organize my code so this is something I yet need to decide.

    Thanks,

    Miguel

     

     

  • Re: Create HTML.Helper

    09-09-2008, 12:47 PM
    • Member
      746 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 147

    Hi Miguel,

    To be honest there is no technical reason not to do what you have suggested. It is my personal preference try to me absolutely clear when it is my code that is being invoked rather than framework code. Also I wouldn't want my co-workers blaming bugs in my custom code on the framework by accident.

    But these kinds of decisions really are your choice, if the convenience of not having to reference an extra namespace makes a difference to you then there is nothing to stop you.

    As the HtmlHelper will used almost exclusively in your view code you could simply add an entry to your web.config to save you referencing the namespace explicitly in every view

        <pages>
          <namespaces>
            <add namespace="Miguel.Mvc"/>
          </namespaces>
        </pages>

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
Page 1 of 1 (10 items)