简体   繁体   English

具有CMS功能的ASP.NET MVC3应用程序

[英]ASP.NET MVC3 application with CMS capabilities

I'm learning ASP.NET MVC3 and doing an application with CMS capabilities - but have run into some problems. 我正在学习ASP.NET MVC3并在做具有CMS功能的应用程序-但是遇到了一些问题。

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). 如果我只使用字符串值,但我希望能够键入所有属性(String,LongString,XhtmlString,Number和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. 在我从Russ得到的答案的帮助下,我想到了以下方法-可行。 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();
    }

}

In MVC, you are supposed to keep property values in your Model, not in your View. 在MVC中,应该将属性值保留在模型中,而不是在视图中。

Read about Model-View-Controller at http://www.asp.net and http://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdf . http://www.asp.nethttp://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdf上了解有关Model-View-Controller的信息

Good luck! 祝好运!

Assuming that the model passed to the strongly typed View is of type Page , then it should simply be @Model.Properties . 假设传递给强类型View的模型的类型为Page ,则它应该只是@Model.Properties So your Razor view looks something like 所以您的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>
}

In order to index into Properties collection using a string, I'd recommend using a Dictionary<string, PropertyDefinition> instead of a List<PropertyDefintion> , or make it a KeyedCollection<TKey, TItem> . 为了使用字符串索引到Properties集合中,我建议使用Dictionary<string, PropertyDefinition>而不是List<PropertyDefintion> ,或者将其KeyedCollection<TKey, TItem>

You might decide to create your own WebViewPage that adds a Page property by inheriting from System.Web.Mvc.WebViewPage and then setting this as the baseType in the web.config under the Views folder 您可能决定通过从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>

Have you considering looking at existing CMS application code for inspiration. 您是否正在考虑查看现有的CMS应用程序代码以获取启发。 You may want to look at projects such as Orchard or even Oxite which is no longer actively developed but can still give insight. 您可能希望查看Orchard或什至Oxite等项目,这些项目已不再积极开发,但仍可以提供见识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM