【问题标题】:ASP.NET MVC3 application with CMS capabilities具有 CMS 功能的 ASP.NET MVC3 应用程序
【发布时间】:2012-01-20 06:46:08
【问题描述】:

我正在学习 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();
    }

}

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-3 content-management-system


    【解决方案1】:

    在 MVC 中,您应该将属性值保留在模型中,而不是视图中。

    http://www.asp.nethttp://www.manning.com/palermo3/ASP.NETMVC3iA_meap_ch01.pdf 了解模型-视图-控制器。

    祝你好运!

    【讨论】:

    • 嗨,我想你误解了我的问题。我想将我的模型传递到调用@model.Property["propertyname"] 的视图中,我只是在写@Page.Property["propertyname"] 时不清楚。我不知道如何将模型绑定在一起以便能够传递具有所有属性的 Page 模型
    【解决方案2】:

    假设传递给强类型视图的模型是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&lt;string, PropertyDefinition&gt; 而不是List&lt;PropertyDefintion&gt;,或者将其设为KeyedCollection&lt;TKey, TItem&gt;

    您可能决定创建自己的 WebViewPage,通过从 System.Web.Mvc.WebViewPage 继承添加 Page 属性,然后将其设置为 Views 文件夹下 web.config 中的 baseType

    <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>
    

    【讨论】:

    • 这正是我一直在寻找的答案——但你仍然有一个问题。如何让字典将 PropertyDefinition 与 PropertyValue 映射?如果我有一个名为“Test”的定义并输入“NormalString”,我希望能够查找 Page().Properties["Test"] 并从 PropertyValue 中获取值
    【解决方案3】:

    您是否考虑查看现有的 CMS 应用程序代码以获取灵感。您可能想查看 Orchard 甚至 Oxite 等项目,这些项目不再积极开发,但仍然可以提供洞察力。

    【讨论】:

    • 是的,我有,但我找不到我要找的东西。在我在这里问之前几周一直试图让这个工作自己工作:)
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 2011-07-30
    • 2011-03-27
    相关资源
    最近更新 更多