English 中文(简体)
使用Html.EditorFor生成具有特定行数和列数的文本区域
原标题:Using Html.EditorFor to generate a textarea with a specific number of rows and columns

我知道您可以将DataType属性与EditorFor html帮助程序一起使用,以指定模型实体的特定属性应显示为多行输入字段。

如果我想指定文本区域必须具有的行数和列数,该怎么办?

在模型中:

[DataType(DataType.MultilineText)]
public string HTMLText { get; set; }

在视图中:

@Html.EditorFor(x => x.HTMLText)

想要的结果:

<textarea id="HTMLText" rows="10" cols="40">value</textarea>

有没有一种方法可以在不使用@Html.Textarea()帮助程序的情况下生成这种代码?

最佳回答

不知道如何设置行和列,但可以使用.multi-line类更改这些文本区域的CSS。当使用EditorFor时,该类会添加到文本区域,因此您可以在该类中指定宽度和高度以获得所需的尺寸。

.multi-line { height:5em; width:5em; }
问题回答

您可以在Razor/c#ie中指定行和列的属性大小和字符等效值:

@Html.TextAreaFor(model => model.Longtext, new { htmlAttributes = new { @class = "ctrl-col2 ctrl-col1", @row = "4", @col = "30" } })

It looks like this is no longer required on MVC 5 when you have @Html.EditorFor with DataType.MultilineText field.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签