AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Thursday 7 January 2016

Use of Custom Html Helpers in ASP.NET MVC

HTML helpers are methods that return HTML string. You can create your own custom HTML helper that will use to return more complex string. Please read below examples where I display custom date format in view.
Setp 1: Add new HtmlHelper Class
public static class MyHtmlHelper
    {
        
        public static string DisplayLocalTime(this HtmlHelper helper, DateTime? UTCtime)
        {
            DateTime _utctime;
            if (UTCtime == null)
            {
                return "";
            }
            else
            {
                _utctime = (DateTime)UTCtime;
            }
            _utctime = _utctime;
            return String.Format("{0}", _utctime.ToString("dd'/'MMM'/'yyyy hh':'mm':'ss tt"));

        }
    }

Step 2: In view page call that Html Helper as
MyHtmlHelper.DisplayLocalTime(Html, DateTime.Now) 

Time will display as shown in below image.

0 comments :

Post a Comment

  • Popular Posts
  • Comments