Aug 16, 2014, 8:22 PM
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
Set Time
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" }
)
)
)
Modelusing 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 Timepublic 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
Last edited by matrixwebtech; Nov 05, 2014 at 12:09 PM.