[英]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.