ASP.NET MVC Extensions Methods

These are my extensions.

  1. Not display dropdownlist if the Select list is null
    public static MvcHtmlString DropDownListNull(this HtmlHelper html,string Name,SelectList select,object htmlAttributes)
    {
    
    if (select == null)
    return MvcHtmlString.Empty;
    else
     return html.DropDownList(Name,select,htmlAttributes);
    
    }
    
    
  2. Transform Enum to SelectList
    #region enum to list
            private static List<KeyValuePair<long,string>> FromEnum(this Type e)
            {
                List<KeyValuePair<long,string>> kvp = new List<KeyValuePair<long,string>>();
                foreach (var s in Enum.GetValues(e))
                {
                    kvp.Add(new KeyValuePair<long,string>((int)s,s.ToString()));
                }
                return kvp;
            }
            public static SelectList ToSelectList(this Type enumObj)
            {
    
                return new SelectList(enumObj.FromEnum(),"Key","Value");
    
            }
            #endregion
    
  3. Transform Generic list into a SelectList
                  #region Generic List to SelectItem
            public static SelectList SelectFromList<TItem>(this List<TItem> values,
                Expression<Func<TItem,string>> key,Expression<Func<TItem,string>> value)
            {
                var Key = key.Compile();
                var Value = value.Compile();
                List<KeyValuePair<string,string>> kvp = new List<KeyValuePair<string,string>>(values.Count);
                values.ForEach(item =>
                    kvp.Add(new KeyValuePair<string,string>(Key.Invoke(item),Value.Invoke(item))));
    
                return new SelectList(kvp,"Key","Value");
    
            }
            #endregion
    
    
  4. Display pager control
    #region paged list control
            //after http://geekswithblogs.net/nuri/archive/2009/08/05/mvc-paged-list.aspx
            /// <summary>
            /// Shows a pager control - Creates a list of links that jump to each page
            /// </summary>
            /// <param name="page">The ViewPage instance this method executes on.</param>
            /// <param name="pagedList">A PagedList instance containing the data for the paged control</param>
            /// <param name="controllerName">Name of the controller.</param>
            /// <param name="actionName">Name of the action on the controller.</param>
            public static void ShowPagerControl(this ViewPage page,BasePagedListImplementation pagedList,string formatUrl)
            {
                HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output);
                if (writer != null)
                {
                    for (int pageNum = 1; pageNum <= pagedList.PageCount; pageNum++)
                    {
                        bool samepage=(pageNum == pagedList.PageNumber);
                        if (!samepage)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Href,string.Format(formatUrl,pageNum));
                            writer.AddAttribute(HtmlTextWriterAttribute.Alt,"Page " + pageNum);
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                        }
    
                        writer.AddAttribute(HtmlTextWriterAttribute.Class,
                                            pageNum == pagedList.PageNumber ?
                                                                "pageLinkCurrent" :
                                                                "pageLink");
    
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                        writer.Write(pageNum);
                        writer.RenderEndTag();
    
                        if (!samepage)
                        {
                            writer.RenderEndTag();
                        }
                        writer.Write(" ");
                    }
    
                    writer.Write("(");
                    writer.Write(pagedList.TotalItemCount);
                    writer.Write(" items in all)");
                }
            }
    
            #endregion
    
    

Other extensions available that are good :

  1. http://blog.wekeroad.com/blog/asp-net-mvc-list-helper-extension-method/
  2. http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
  3. http://helios.ca/2009/09/21/asp-net-mvc-extension-methods-of-urlhelper/
  4. http://blog.donnfelker.com/2010/02/25/asp-net-mvc-tempdata-extension-methods/
  5. http://inq.me/post/ASPNet-MVC-Extension-method-to-create-a-Security-Aware-HtmlActionLink.aspx

If you know more,please tell me


Posted

in

,

by

Comments

12 responses to “ASP.NET MVC Extensions Methods”

  1. cna training Avatar

    Keep posting stuff like this i really like it

  2. pjbrain Avatar

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

    1. admin Avatar
      admin

      Thank you!
      My account on twitter is http://twitter.com/ignatandrei

  3. Natalie Avatar

    Very educating story, saved your website for interest to see more!

  4. Dwayne Roxberry Avatar

    Interesting blog post. I have just bookmarked your page. Cheers!

  5. make music online Avatar

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is great blog. A great read. Ill definitely be back.

  6. make beats online Avatar

    very good selection. useful for me. Thanks

  7. Jose Diaz Avatar
    Jose Diaz

    Thanks for sharing, I like this blog!

  8. Madalene Kade Avatar

    There is noticeably a bundle to identify about this. I believe you made various nice points in features also.

  9. Aldis Avatar

    This is exactly the info I desired. Many thanks for writing this post.

  10. Calvin Avatar

    Aw, this is a very good post. In concept I would like to put in writing in this way furthermore – spending time and real effort to have a great write-up

  11. […] key point is MVC2 abstracts the HTML using the built-in (and extensible) HTML helper classes. Seems like it’s time to write one of those to take advantage of […]

Leave a Reply

Your email address will not be published. Required fields are marked *