Asp.Net MVC2.0预览构建提供了如下帮助程序
Html.EditorFor(c => c.propertyname)如果属性名是string,上面的代码会呈现一个文本框。
如果我想将MaxLength和Size属性传递给文本框或我自己的css类属性,该怎么办?
我需要为我的应用程序中的每个尺寸和长度组合创建一个模板吗?如果是这样的话,这并不能使默认模板可用。
发布于 2010-12-25 20:39:57
我写了一篇博客来回答我自己的问题
Adding html attributes support for Templates - ASP.Net MVC 2.0 Beta
发布于 2011-03-10 06:26:31
在MVC3中,您可以按如下方式设置宽度:
@Html.TextBoxFor(c => c.PropertyName, new { style = "width: 500px;" })发布于 2010-06-11 16:08:33
我通过在我的/Views/Shared/EditorTemplates文件夹中创建一个名为String.ascx的EditorTemplate解决了这个问题:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<% int size = 10;
int maxLength = 100;
if (ViewData["size"] != null)
{
size = (int)ViewData["size"];
}
if (ViewData["maxLength"] != null)
{
maxLength = (int)ViewData["maxLength"];
}
%>
<%= Html.TextBox("", Model, new { Size=size, MaxLength=maxLength }) %>在我看来,我使用
<%= Html.EditorFor(model => model.SomeStringToBeEdited, new { size = 15, maxLength = 10 }) %>对我来说就像是一种魔力!
https://stackoverflow.com/questions/1625327
复制相似问题