asp.net – 如何创建一个HTML Helper来扩展TextBoxFor()以添加CS
发布时间:2020-12-16 00:06:09 所属栏目:asp.Net 来源:网络整理
导读:如何创建一个HTML Helper来扩展TextBoxFor()以添加CSS样式? @Html.TextBoxFor(model = model.FirstName,new { @class = "txt" }) 解决方法 public static System.Web.Mvc.MvcHtmlString DtxTextBoxForTModel,TValue (this System.Web.Mvc.HtmlHelperTModel
如何创建一个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 Zero轻量级审核流设计
- asp.net-mvc – ASP.NET MVC – 我可以为同一个动作创建多个
- ASP页面的ASP.NET cookie
- asp.net中User.Identity中的Ticket.UserData有多安全
- ASP.NET MVC – 单元测试覆盖初始化方法
- asp.net-mvc – Sitecore搜索方面和计算字段
- asp.net-mvc – 强类型视图与动态类型视图
- asp.net-mvc – 我必须附加什么进程才能使用IIS Express配置
- ASP.Net MVC cookies – 防篡改?
- asp.net-mvc-3 – 方法’Write’没有重载需要0个参数
推荐文章
站长推荐
- asp.net-core – 如何在ASP.NET Core的OpenIdCon
- adfs2.0 – 启用了非声明的ASP.NET应用程序和ADF
- .net – 需要临时报告组件
- asp.net-core – 在.NET Core Web API上为CORS启
- asp.net-mvc – 为什么要使用MVC帖子注销?
- ASP.NET应用程序通过Windows身份验证或表单身份验
- kendo-ui – 剑道多选控制 – 从下拉列表中删除所
- ASP.Net表单身份验证 – 在超时时转到登录页面
- IIS 7.5应用程序初始化ASP.NET Web服务(预热),而
- asp.net-mvc-4 – 在ASP.NET MVC4 WebAPI中实现E
热点阅读