asp.net-mvc – MVC3,Razor,Html.TextAreaFor():调整高度以适应
发布时间:2020-12-16 07:28:10 所属栏目:asp.Net 来源:网络整理
导读:我目前在视图中使用以下代码来调整 Html.TextAreaFor()的高度以适应其内容.有没有明显更好的 /更简洁的方法来做到这一点? ...int width = 85;int lines = 1;string[] arr = Model.Text.Split(new string[] {"rn","n","r"},StringSplitOptions.None);for
我目前在视图中使用以下代码来调整
Html.TextAreaFor()的高度以适应其内容.有没有明显更好的& /更简洁的方法来做到这一点?
... int width = 85; int lines = 1; string[] arr = Model.Text.Split(new string[] {"rn","n","r"},StringSplitOptions.None); foreach (var str in arr) { if (str.Length / width > 0) { lines += str.Length / width + (str.Length % width <= width/2 ? 1 : 0); } else { lines++; } } @Html.TextAreaFor(m => m.Text,new { id = "text",style = "width:" + width + "em; height:" + lines + "em;" }) ... 解决方法
代码看起来很好.一种可能的改进是将其外部化为可重用的帮助程序,以避免污染视图:
public static class TextAreaExtensions { public static IHtmlString TextAreaAutoSizeFor<TModel,TProperty>( this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes ) { var model = ModelMetadata.FromLambdaExpression(expression,htmlHelper.ViewData).Model; var text = model as string ?? string.Empty; int width = 85; int lines = 1; string[] arr = text.Split(new string[] { "rn","r" },StringSplitOptions.None); foreach (var str in arr) { if (str.Length / width > 0) { lines += str.Length / width + (str.Length % width <= width / 2 ? 1 : 0); } else { lines++; } } var attributes = new RouteValueDictionary(htmlAttributes); attributes["style"] = string.Format("width:{0}em; height:{1}em;",width,lines); return htmlHelper.TextAreaFor(expression,attributes); } } 并在视图中: @Html.TextAreaAutoSizeFor(m => m.Text,new { id = "text" }) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 自定义ActionLink帮助器,它知道你正在使用哪
- ASP.NET发布尝试复制不存在的文件
- asp.net – 用于验证的数据注释,至少一个必填字段?
- .net – 简单注入器“由于在MVC验证期间已经处理了DbContex
- asp.net – MVC IIS7 = CSS问题
- 如何在ASP.NET自定义控件中持久保存List属性?
- asp.net – C# – 一次将多个记录插入AS400
- asp.net – 我的应用程序让我重定向到Account / LogOn?Ret
- asp.net-mvc – MVC授权属性HttpUnauthorizedResult FormsA
- asp.net-core – ASP.NET Core App中project.json中的build
推荐文章
站长推荐
- asp.net-core – 如何使用TeamCity使用WebDeploy
- asp.net-mvc – 使用数据注释进行自定义验证
- asp.net – 命名空间’System.Web’中不存在类型
- asp.net-mvc – 奇怪的错误w / NinjectValidator
- .net – 为什么要使用asp:HyperLink
- asp.net – 尝试的转移似乎包含数据泄漏?
- ASP.NET MVC4 WebAPI和Posting XML数据
- asp.net-mvc – 向MVC 3添加基于声明的授权
- .net-core – 存储Azure Vault客户端ID和客户端密
- ASP.NET MVC> ASP.NET WebForms,为什么?
热点阅读