Custom Time field MVC Razor using partial-view

  1. #1

    Custom Time field MVC Razor using partial-view

    Hi all, I prepare a very basic custom time field,I will share this here,any body have any suggestion or advice fore code improvement please post.
    may it will help some one.


    view

    @model UserControls.Models.TimeModel
    @{
        var X = Html.X();
    }
    <script>
        var hhValidation = function (h) {
           
            if (h.getValue() > 12) {
                h.setValue(12);
            }
           
        }
    
        var mmValidation = function (m) {
            if (m.getValue() > 59) {
                m.setValue(59);
            }
          
        }
    
    
    
    </script>
    
    
    
            
    
    @(X.Container().LayoutConfig(new HBoxLayoutConfig { Align = HBoxAlign.StretchMax }).Border(false).Width(200).Padding(0)
    .Items
    (
            
           
             X.TextFieldFor(o=>o.HH).Width(24).LabelWidth(20).MaxLength(2).Text("30")
            .AllowBlank(false).MaskRe(@"/[0-9\$\.]/").HideLabel(true)
                .Listeners(l => { l.Change.Handler = "hhValidation(this);"; })
            ,
    
            X.DisplayField().Text(":"),
    
            X.TextFieldFor(o=>o.MM).Width(24).LabelWidth(20).MaxLength(2).Text("30")
            .AllowBlank(false).MaskRe(@"/[0-9\$\.]/").HideLabel(true)
            .Listeners(l => { l.Change.Handler = "mmValidation(this);"; })
            ,
    
            X.DisplayField().Text(":"),
    
            X.ComboBoxFor(o=>o.TT).Width(45).LabelWidth(20).HideLabel(true).Editable(false).AllowBlank(false)
            .SelectedItems("AM")
    
            .Items(
            new ListItem { Text = "AM" },
            new ListItem { Text = "PM" }
                   )
    
     
     
    )
        )
    Model

    using System;
    using Ext.Net;
    namespace UserControls.Models
    {
        public class TimeModel
        {
            public string s = string.Empty;
            
            public string HH
            {
                get {return  X.GetCmp<NumberField>(s + ".HH").Value.ToString(); }
                set { X.GetCmp<NumberField>(s + "_HH").Value = value; }
            }
    
    
            public string MM
            {
                get { return Convert.ToString(X.GetCmp<NumberField>(s + ".MM").Value); }
                set { X.GetCmp<NumberField>(s + "_MM").Value = value; }
            }
    
    
            public string TT
            {
                get { return Convert.ToString(X.GetCmp<ComboBox>(s + ".TT").Value); }
                set { X.GetCmp<ComboBox>(s + "_TT").Value = value; }
            }
    
            public string gettime()
            {
    
                string strRes = string.Empty;
                string strHH = string.Empty;
                string strMM = string.Empty;
                string strTT = string.Empty;
    
                strHH = HH;
                strMM = MM;
                strTT = TT;
    
                if (strHH.Length < 2)
                {
                    strHH = "0" + strHH;
                }
                if (strMM.Length < 2)
                {
                    strMM = "0" + strMM;
                }
                strRes = strHH + ":" + strMM + " " + strTT;
    
    
                return strRes;
            }
    
            public void settime(string strtime)
            {
                string[] str1st = strtime.Split(':');
                string[] str2nd = str1st[1].Split(' ');
    
                HH = str1st[0];
                MM = str2nd[0];
                TT = str2nd[1];
    
               
            }
    
    
        }
    }
    CallPartial view in View

     X.DisplayField().Text("Application End Time: ") ,
                                     
                                     X.Container()
                                     
                                    .ItemsFromPartial(ERP.Views.view.uc_Time,new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix ="et" } })
                                    )
    If you want to use this more than one in same view ,add unique "HtmlFieldPrefix" ,It will prevent auto generation of ID.

    Set Time


     public ActionResult Index()
            {
               UserControls.Models.TimeModel _time = new UserControls.Models.TimeModel();
    
        
                _time.s = "et";
                _time.settime("7:00 PM");
              
                return View();
            }
    Get Time

    public DirectResult gettime()
            {
    UserControls.Models.TimeModel _time = new UserControls.Models.TimeModel();
    _time.s = "st";
         DateTime d = new DateTime();
    d= Convert.ToDateTime(_time.gettime());
    }
    Sample Screen shot attached

    Click image for larger version. 

Name:	time.png 
Views:	20 
Size:	2.4 KB 
ID:	14531
    Last edited by matrixwebtech; Nov 05, 2014 at 12:09 PM.
  2. #2
    Hi @matrixwebtech,

    Thank you for sharing!

    I am moving the thread to the Examples and Extras forum.
  3. #3
    thanks Daniil.Is coding part is ok or need some improvement?
    Last edited by matrixwebtech; Nov 05, 2014 at 12:09 PM.
  4. #4
    I think if it works for you, then it is good enough.

Similar Threads

  1. Replies: 2
    Last Post: Feb 06, 2014, 1:19 PM
  2. [CLOSED] Using Razor Partial View on Non-Razor View
    By Patrick_G in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 26, 2013, 4:09 AM
  3. Replies: 1
    Last Post: Dec 03, 2012, 1:57 AM
  4. Replies: 4
    Last Post: Apr 09, 2012, 2:10 PM
  5. [CLOSED] Razor syntax for adding a partial view to a Panel
    By machinableed in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Feb 23, 2012, 9:55 AM

Posting Permissions