TextBoxFor()扩展方法
1.扩展方法
public static class TextBoxForHelper
{
public static MvcHtmlString CreateTextBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, DateTime?>> expression)
{
//获取文本框Name值
var tempName = expression.Body.ToString();
var name = tempName.Substring(2);
//"p.CreateDate"
DateTime dateTime = new DateTime();
var date = htmlHelper.ViewData.Eval(name);
if (date != null)
{
dateTime = Convert.ToDateTime(date);
}
return htmlHelper.TextBoxFor(expression, new
{
onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd'})",
@class = "Wdate mws-textinput",
style = "height: 30px;",
Value = dateTime.ToString("yyyy-MM-dd") == "0001-01-01" ? DateTime.Now.ToString("yyyy-MM-dd") : dateTime.ToString("yyyy-MM-dd")
});
}
2.页面调用
@Html.CreateTextBoxFor(p=>p.CreateDate)
posted on 2015-01-30 17:07 CeleryCabbage 阅读(359) 评论(0) 编辑 收藏 举报