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