Я изучаю 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"]
иполучить значения.Я застрял с вышеизложенным, и не знаю, как действовать.
Редактировать:
С некоторой помощью из ответа, который я получил от Руси, я пришелсо следующим - который работает.Не знаете, есть ли лучшее решение проблемы.
У кого-нибудь есть предложения по улучшению моего решения?
Модели:
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();
}
}