0

只是想知道是否有人对我们如何设置模板样式有任何想法......就像如果我想更改文本框的 MaxLength 我该怎么做。

我希望能够做的类似于我在 WPF 中使用样式和模板可以做的事情......在 WPF 中,您可以将样式传递给模板,然后选择应用这些样式的位置......例如,如果用户在控件(即模板)上设置宽度,在控件代码中我可以选择将该宽度应用于模板中我想要的任何元素或根本不应用......

因此,我想知道是否有人知道类似的事情?

4

3 回答 3

2

我在这里发布了一个类似问题的答案。

如果您想要通用样式,您可以从支持您所需样式的基本 TemplateViewModel 类派生自定义模板的模型:

public interface ITextSpecifier
{
  int? Size { get; }
  bool AutoGrow { get; }
}

public class TemplateViewModel<T> where T: class
{
  public IDictionary<string, string> Attributes { get; }
  public ITextSpecifier TextStyle { get; private set; }
  public IColorSpecifier ColorStyle { get; }
  public T TextStyle(int size, bool autogrow)
  {
     TextStyle = new TextSpecifier(size, autogrow);
     return this;
  }
}

public class TextBoxViewModel: TemplateViewModel<TextBoxViewModel>
{
}

<%= Html.EditorFor(x => new TextBoxViewModel(Model.StringData).TextStyle(10, false)) %>

在模板中:

<!-- template page derived from typed control for TextBoxViewModel -->
<input type='text' <%= Model.TextStyle.Size != null 
    ? "size='" + Model.TextStyle.Size + "'" : "" %> ... />

这有点工作,这就是为什么我希望他们会在 MVC v2 版本中发明一些通用方法。

于 2009-10-30T18:37:25.207 回答
0

这里的解决方案是使用元数据!您可以在此处重用 StringLength 数据注释验证属性。创建一个新的元数据提供程序,从 StringLength 属性中读取最大长度并将其放入 AdditionalValues 字典。您的元数据提供程序应继承自 DataAnnotationsModelMetadataProvider。然后在您创建的模板中,在 DisplayTemplates 或 EditorTemplates 中,您可以访问字符串的最大长度。

于 2010-05-07T22:57:27.873 回答
0

据我所知,最简单的方法是为文本框创建一个 HTML id,如下所示:

  @Html.EditorFor(model => model.Description, null, "Description", null)

And at the end of the page, write the following javascript

 <script type="text/javascript">
        $(function () { $("#Description").css("width","450"); });    
 </script>

NB:- Even I hate MVC!!!

于 2012-07-13T22:55:01.077 回答