I’m learning ASP.NET MVC3 and doing an application with CMS capabilities – but have run into some problems.
The basic idea is that all pages are connected to a pagetype, which in turn have a set of properties connected to it.
Works great if I’m only using string values, but I want to be able to type all properties (String, LongString, XhtmlString, Number and TrueFalse).
This is my models that I have come up with.
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; }
}
Basically, when I’m in my View i want to be able to call for example
@Page.Property["Whatever"]
and get the values. I’m stuck with the above, and don’t know how to proceed.
Edit:
With some help from the answer I got from Russ, I came up with the following – which works.
Don’t know if there is any better solution to the problem.
Any one have any suggestions on how to improve my solution?
Models:
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; }
}
Controller:
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();
}
}
Assuming that the model passed to the strongly typed View is of type
Page, then it should simply be@Model.Properties. So your Razor view looks something likeIn order to index into
Propertiescollection using a string, I’d recommend using aDictionary<string, PropertyDefinition>instead of aList<PropertyDefintion>, or make it aKeyedCollection<TKey, TItem>.You might decide to create your own WebViewPage that adds a
Pageproperty by inheriting fromSystem.Web.Mvc.WebViewPageand then setting this as the baseType in the web.config under the Views folder