asp.net – 如何创建一个HTML Helper来扩展TextBoxFor()以添加CSS样式?
发布时间:2020-05-25 05:04:12 所属栏目:asp.Net 来源:互联网
导读:如何创建一个HTML Helper来扩展TextBoxFor()以添加CSS样式? @Html.TextBoxFor(model = model.FirstName, new { @class = txt }) public static System.Web.Mvc.MvcHtmlString DtxTextBoxForTModel, TValue (this Syst
|
如何创建一个HTML Helper来扩展TextBoxFor()以添加CSS样式? @Html.TextBoxFor(model => model.FirstName,new { @class = "txt" })
解决方法public static System.Web.Mvc.MvcHtmlString DtxTextBoxFor<TModel,TValue>
(this System.Web.Mvc.HtmlHelper<TModel> html,System.Linq.Expressions.Expression<System.Func<TModel,TValue>> expression,System.Collections.Generic.IDictionary<string,object> htmlAttributes = null,bool readOnly = false)
{
if (htmlAttributes == null)
{
htmlAttributes =
new System.Collections.Generic.Dictionary<string,object>();
}
System.Web.Mvc.ModelMetadata oModelMetadata =
System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression,html.ViewData);
if (oModelMetadata == null)
{
if (readOnly)
{
if (htmlAttributes.ContainsKey("readonly") == false)
{
htmlAttributes.Add("readonly","read-only");
}
}
}
else
{
if (htmlAttributes.ContainsKey("placeholder") == false)
{
string strHtmlFieldName =
System.Web.Mvc.ExpressionHelper.GetExpressionText(expression);
string strLabelText =
oModelMetadata.DisplayName ??
oModelMetadata.PropertyName ??
strHtmlFieldName.Split('.').Last();
if (string.IsNullOrEmpty(strLabelText) == false)
{
htmlAttributes.Add("placeholder",strLabelText);
}
}
if ((readOnly) || (oModelMetadata.IsReadOnly))
{
if (htmlAttributes.ContainsKey("readonly") == false)
{
htmlAttributes.Add("readonly","read-only");
}
}
}
htmlAttributes.Add("class","form-control");
System.Linq.Expressions.MemberExpression oMemberExpression =
expression.Body as System.Linq.Expressions.MemberExpression;
if (oMemberExpression != null)
{
System.ComponentModel.DataAnnotations.StringLengthAttribute oStringLengthAttribute =
oMemberExpression.Member.GetCustomAttributes
(typeof(System.ComponentModel.DataAnnotations.StringLengthAttribute),false)
.FirstOrDefault() as System.ComponentModel.DataAnnotations.StringLengthAttribute;
if (oStringLengthAttribute != null)
{
if (htmlAttributes.ContainsKey("maxlength") == false)
{
htmlAttributes.Add("maxlength",oStringLengthAttribute.MaximumLength);
}
}
}
return (html.TextBoxFor(expression,htmlAttributes));
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Chrome在成功的文件下载(200状态)上显示“已取消
- asp.net-mvc – 如何在ASP.Net MVC 3中可选地呈现一个部分?
- asp.net-mvc – Ninject.Extensions.Logging.nlog2 – 如何
- asp.net-mvc – 无法导出Kendo Grid中的隐藏列
- 如何在页面加载后从代码设置ASP.NET标签文本?
- asp.net-mvc – 如何使用ASP.NET 5 MVC保护Web API 6
- asp.net – 在Web.config中是否可以在指定目录中注册所有用
- 如何在Asp.NET MVC中干净地重用编辑/新视图
- 将ASP.NET应用程序本地化为普通话
- asp.net-mvc – 如何GetBytes()在C#与UTF8编码与BOM?
推荐文章
站长推荐
- asp.net-mvc – MVC 4 Code First属性上的Foreig
- asp.net-mvc – 为FileResult设置ETag – MVC 3
- asp.net – 使用vb.net和EPPlus的单元格包装
- asp.net-mvc – MVC帮助 – 使用@URL的图像src?
- asp.net-mvc – 如何将NUnit作为ASP.NET MVC的测
- asp.net – SignalR Autofac OWIN:为什么Global
- ASP.NET Core RC2无法找到Html编码器实现
- 我可以使用ASP.NET Core仅针对.NET 4.6.1吗?
- asp.net-mvc-3 – 如何拦截MVC应用程序中的所有控
- 以编程方式获取当前的ASP.NET信任级别
热点阅读
