PDA

View Full Version : Custom Time field MVC Razor using partial-view



matrixwebtech
Aug 16, 2014, 8:22 PM
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).Edi table(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

14531

Daniil
Aug 18, 2014, 1:51 PM
Hi @matrixwebtech,

Thank you for sharing!

I am moving the thread to the Examples and Extras forum.

matrixwebtech
Aug 18, 2014, 3:07 PM
thanks Daniil.Is coding part is ok or need some improvement?

Daniil
Aug 18, 2014, 3:24 PM
I think if it works for you, then it is good enough.