[英]ASP.NET MVC3 application with CMS capabilities
我正在學習ASP.NET MVC3並在做具有CMS功能的應用程序-但是遇到了一些問題。
基本思想是所有頁面都連接到一個頁面類型,而頁面類型又具有一組與其連接的屬性。
如果我只使用字符串值,但我希望能夠鍵入所有屬性(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"]
並獲取值。 我堅持以上,並且不知道如何進行。
編輯:
在我從Russ得到的答案的幫助下,我想到了以下方法-可行。 不知道是否有更好的解決方案。
有人對如何改善我的解決方案有任何建議嗎?
楷模:
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();
}
}
在MVC中,應該將屬性值保留在模型中,而不是在視圖中。
在http://www.asp.net和http://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdf上了解有關Model-View-Controller的信息 。
祝好運!
假設傳遞給強類型View的模型的類型為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>
。
您可能決定通過從System.Web.Mvc.WebViewPage
繼承,然后在Views文件夾下的web.config中將其設置為baseType,來創建添加Page
屬性的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>
您是否正在考慮查看現有的CMS應用程序代碼以獲取啟發。 您可能希望查看Orchard或什至Oxite等項目,這些項目已不再積極開發,但仍可以提供見識。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.