为了在 MVC 中做一个简单的内容管理系统,您可能想要组织事物,使页面模型是内容项的列表,以便您的视图迭代内容项并显示它们
public partial class Content
{
public Content()
{
this.Pages = new HashSet<Page>();
}
public int ContentID { get; set; }
public string ContentTitle { get; set; }
public string ContentImage { get; set; }
public string ContentImageAlt { get; set; }
public string ContentTitleLink { get; set; }
public string ContentImageLink { get; set; }
public string ContentBody { get; set; }
public string ContentTeaser { get; set; }
public System.DateTime ContentDate { get; set; }
public bool enabled { get; set; }
public int SortKey { get; set; }
public int ContentTypeID { get; set; }
public virtual ContentType ContentType { get; set; }
public virtual ICollection<Page> Pages { get; set; }
}
视图很简单
@foreach (var art in Model.Content)
{
<text>
@Html.DynamicPageContent(art)
</text>
}
而使用的助手是
public static MvcHtmlString DynamicPageContent(this HtmlHelper helper, Content content)
{
if (content.ContentType==null) return new MvcHtmlString(content.ContentBody);
return content == null ? null : MvcHtmlString.Create( String.Format("\n<!--{0}: {1}({2})-->\n",content.ContentID, content.ContentType.ContentTypeDescription, content.ContentTypeID)+helper.Partial(content.ContentType.TemplateName, content).ToString().Trim());
}
其中每个 Content.ContentType 都包含一个 TemplateName,它是一个 MVC 视图名称。
所以主视图然后渲染了一些部分视图。我的部分视图中最简单的只包含@Html.Raw(content.Body),其他视图则使用 Content 类的属性呈现更多结构化内容:我有一个用于托管图像,一个用于新闻文章等。
然后在您的后端,您可以使用 Kendo 控件(或其他控件)来编辑 ContentBody、ContentTeaser 等,只需设置一个适当的 ContentType 来命名部分视图以呈现它。
希望这会给你足够的帮助让你开始。