2011-12-14 33 views
2

我正在學習ASP.NET MVC3並使用CMS功能執行應用程序 - 但遇到了一些問題。具有CMS功能的ASP.NET MVC3應用程序

其基本思想是所有頁面都連接到一個pagetype,而該pagetype又有一組屬性連接到它。

很好,如果我只使用字符串值,但我想能夠鍵入所有屬性(字符串,LongString,XhtmlString,數字和TrueFalse)。

這是我想出的模型。

public class Page 
{ 
    public int PageId { get; set; } 
    public int PageTypeId { get; set; } 
    public string Name { get; set; } 

    public PageType PageType { get; set; } 

    public List<PropertyValues> Properties { get; set; } 
} 

public class PageType 
{ 
    public int PageTypeId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public string Controller { get; set; } 
    public string Action { get; set; } 

    public List<PropertyDefinition> Properties { get; set; } 
} 

public class PropertyDefinition 
{ 
    public int PropertyDefinitionId { get; set; } 
    public string Name { get; set; } 
    public string Type { get; set; } // NormalString, LongString, XhtmlString, Number, TrueFalse 
} 

public class PropertyValues 
{ 
    public int PropertyValueId { get; set; } 
    public int PageId { get; set; } 
    public int PropertyDefinitionId { get; set; } 

    public string ValueNormalString { get; set; } // input=text 
    public string ValueLongString { get; set; } // textarea 
    public string ValueXhtmlString { get; set; } // tinymce 
    public int ValueNumber { get; set; } // input=text 
    public bool ValueTrueFalse { get; set; } // input=checkbox 

    public PropertyDefinition Definition { get; set; } 
} 

基本上,當我在我看來是我希望能夠調用例如

@Page.Property["Whatever"] 

,並得到的值。我被困在上面,不知道如何繼續。

編輯:

從我從拉斯得到的答案一定的幫助,我想出了以下 - 這工作。 不知道是否有更好的解決方案。

任何人對如何改善我的解決方案有任何建議嗎?

型號:

public class Page 
{ 
    public int PageId { get; set; } 
    public string Name { get; set; } 

    public List<Property> Properties { get; set; } 

    Dictionary<string, object> PageData = new Dictionary<string, object>(); 
    public object this[string name] 
    { 
     get 
     { 
      Property result = this.Properties.Find(
       delegate(Property p) 
       { 
        return p.Name.Equals(name); 
       }); 

      if (result != null) 
      { 
       switch (result.PropertyDefinition.Type) 
       { 
        case "String": 
         return result.PropertyValue.ValueString; 
        case "LongString": 
         return result.PropertyValue.ValueLongString; 
        case "XHtmlString": 
         return result.PropertyValue.ValueXhtmlString; 
        case "Number": 
         return result.PropertyValue.ValueNumber; 
        case "Boolean": 
         return result.PropertyValue.ValueBoolean; 
        default: 
         return null; 
       } 
      } 

      return null; 
     } 
     set 
     { 
      PageData[name] = value; 
     } 
    } 
} 

public class PropertyDefinition 
{ 
    public int PropertyDefinitionId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Type { get; set; } 
} 

public class PropertyValue 
{ 
    public int PropertyValueId { get; set; } 
    public int PropertyId { get; set; } 

    public string ValueString { get; set; } 
    public string ValueLongString { get; set; } 
    public string ValueXhtmlString { get; set; } 
    public int ValueNumber { get; set; } 
    public bool ValueBoolean { get; set; } 
} 

public class Property 
{ 
    public int PropertyId { get; set; } 
    public string Name { get; set; } 
    public string Caption { get; set; } 

    public Page Page { get; set; } 
    public PropertyDefinition PropertyDefinition { get; set; } 
    public PropertyValue PropertyValue { get; set; } 

    public int PageId { get; set; } 
    public int PropertyDefinitionId { get; set; } 
    public int PropertyValueId { get; set; } 
} 

控制器:

public class HomeController : Controller 
{ 
    public String Index() 
    { 
     List<Property> properties = new List<Property>(); 
     Property propertyOne = new Property { 
      PropertyId = 1, 
      Name = "Title", 
      Caption = "Page Title", 
      TestPageId = 1, 
      PropertyValue = new PropertyValue { 
       PropertyValueId = 1, 
       PropertyId = 1, 
       ValueString = "This is the page title" 
      }, 
      PropertyDefinition = new PropertyDefinition { 
       PropertyDefinitionId = 1, 
       Name = "String", 
       Description = "String (< 255 characters)", 
       Type = "String" 
      } 
     }; 
     properties.Add(propertyOne); 

     Page CurrentPage = new Page 
     { 
      TestPageId = 1, 
      Name = "Testpage", 
      Properties = properties 
     }; 


     return CurrentPage["Title"].ToString(); 
    } 

} 

回答

0

假設傳遞給強類型視圖的模型的類型爲Page,那麼它應該簡單地爲@Model.Properties。所以,你的Razor視圖使用字符串類似

@* set the model for the view to be of type Page *@ 
@model Page 

@* to get the properties *@ 
@foreach(var prop in Model.Properties) 
{ 
    <p>@prop.PageId</p> 
} 

爲了索引Properties收集,我建議使用Dictionary<string, PropertyDefinition>代替List<PropertyDefintion>,或使它成爲一個KeyedCollection<TKey, TItem>

您可能決定創建自己的WebViewPage通過從System.Web.Mvc.WebViewPage繼承,然後查看文件夾

<system.web.webPages.razor> 
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
    <!-- Set your type here below --> 
    <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
     <namespaces> 
      <add namespace="System.Web.Mvc" /> 
      <add namespace="System.Web.Mvc.Ajax" /> 
      <add namespace="System.Web.Mvc.Html" /> 
      <add namespace="System.Web.Routing" /> 
      <add namespace="System.Web.Configuration"/> 
      <add namespace="System.ComponentModel.DataAnnotations"/> 
     </namespaces> 
    </pages> 
</system.web.webPages.razor> 
+0

這正是我正在尋找的那種答案 - 但仍然存在一個問題。如何讓字典使用PropertyValue映射PropertyDefinition?如果我有一個名稱爲「測試」並鍵入「NormalString」的定義,我希望能夠查找頁面()。屬性[「測試」],並從PropertyValue獲取值 – Fredrik 2011-12-14 14:34:25

0

在MVC中,你都應該保持的屬性值在模型中,而不是在你的視圖。

閱讀關於模型 - 視圖 - 控制器在http://www.asp.nethttp://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdf

祝你好運!

+0

嗨,我想你已經誤解了我的問題。我想將我的模型傳遞給調用@model的視圖。屬性[「propertyname」]我只是寫了@ Page.Property [「propertyname」]纔不清楚。我不知道如何將模型綁定在一起以便能夠將Page模型與所有屬性相關聯 – Fredrik 2011-12-14 14:25:50

0

下在web.config中設置此爲基本類型添加了一個Page屬性你是否考慮在尋找現有CMS應用程序代碼的靈感。你可能想看一些像Orchard甚至Oxite這樣的項目,這些項目不再積極發展,但仍然可以提供見解。

+0

是的,我有,但我有找不到我要找的東西。一直試圖讓我在這裏問幾個星期前自己工作:) – Fredrik 2011-12-14 16:51:24

相關問題