2009-10-30 42 views
0

只是想知道如果任何人有任何想法,我們如何可以樣式模板...就像如果我想改變文本框的MaxLength我怎麼去做這件事。ASP.net MVC v2 - 造型模板

我希望能夠做的事情就像我可以在WPF中使用樣式和模板所做的一樣...在WPF中,您可以將樣式傳遞給模板,然後選擇這些樣式的應用位置......例如,如果用戶在控件上設置了寬度(這是模板),那麼在控件代碼中,我可以選擇將該寬度應用於模板內的任何元素或根本沒有任何元素...

因此,我想知道有沒有人知道類似的東西?

回答

2

我已發佈回答一個問題here

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 + "'" : "" %> ... /> 

這是一個有點

如果你想通用的樣式,可以從基TemplateViewModel類,這將支持您所需的樣式派生自定義模板的模型的工作,這就是爲什麼我希望他們會在MVC v2發佈中發明一些常用方法。

+0

我可以看到你在...但是正如你在另一篇文章中所說,我認爲需要有一種更好的方式來處理這個問題......我認爲WPF解決方案在這裏工作得很好.. – vdhant 2009-10-30 23:08:06

+0

這看起來可以解決我正在處理的問題。您希望在v2或v3中獲得新的東西嗎? T – kenny 2011-03-04 15:18:04

+0

那麼,我仍然使用v1 ;-) – queen3 2011-03-05 15:54:33

0

這裏的解決方案是使用元數據!您可以在這裏重複使用StringLength數據註釋驗證屬性。創建一個新的元數據提供程序,它從StringLength屬性讀取最大長度並將其放入AdditionalValues字典中。您的元數據提供程序應該從DataAnnotationsModelMetadataProvider繼承。然後在您創建的模板中,在DisplayTemplates或EditorTemplates中,可以訪問該字符串的最大長度。

0

據我知道要做到這一點最簡單的方法是創建一個文本框一個HTML ID像圖所示:

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

,並在頁面的最後,寫了下面的JavaScript

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

注意: - 即使我討厭MVC!

+0

你爲什麼討厭設計模式?或者你是指IE瀏覽器爲「互聯網」並將ASP.NET MVC框架稱爲「mvc」的人之一? – 2012-07-14 00:05:37