2011-12-14 12 views
2

私はASP.NET MVC3を学び、CMS機能を備えたアプリケーションを実行していますが、いくつかの問題が発生しています。CMS機能を備えたASP.NET MVC3アプリケーション

基本的な考え方は、すべてのページがpagetypeに接続されていて、一連のプロパティがpagetypeに接続されていることです。

文字列値のみを使用している場合にはうまく動作しますが、すべてのプロパティ(String、LongString、XhtmlString、Number、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; } 
} 

コントローラー:MVCで

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になります。だからあなたのカミソリビューは、文字列を使用してPropertiesコレクションにインデックスするために

@* 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> 
} 

ようになり、私の代わりにList<PropertyDefintion>Dictionary<string, PropertyDefinition>を使用することをお勧めします、またはそれKeyedCollection<TKey, TItem>作ると思います。あなたは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

これはまさに私が探していた答えの種類ですが、それでも1つの問題があります。 PropertyValueでPropertyDefinitionをマップする辞書を取得するにはどうしたらいいですか? "Test"という名前の定義があり、 "NormalString"と入力した場合、Page()を検索してPropertyValueから値を取得したいのですが – Fredrik

0

は、あなたがいないあなたのビューでは、モデル内のプロパティ値を保つことになっています。

モデルビューコントローラについては、http://www.asp.nethttp://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdfを参照してください。

幸運を祈る!

+0

こんにちは、私はあなたが私の質問を誤解したと思います。 @modelを呼び出すビューにモデルを渡したいと思います。プロパティ["propertyname"] @ Page.Property ["propertyname"]を書いたとき、私は不明でした。私はすべてのプロパティを持つページモデルを渡すことができるようにモデルを結びつける方法を知らない。 – Fredrik

0

既存のを見て検討しましビュー]フォルダの下にあるweb.configにbaseTypeとしてこれを設定することにより、Pageプロパティを追加し、独自のWebViewPageを作成することにしたかもしれません

インスピレーションのためのCMSアプリケーションコード。オーチャードやOxiteのようなプロジェクトは、もはや積極的に開発されていませんが、まだ洞察力を与えることができます。

+0

はい、持っています私が探しているものを見つけられない。ここに質問する前に、これを私自身で数週間働かせるようにしています:) – Fredrik

関連する問題