为了做到在MVC一个简单的内容管理系统,你可能想organiz Ë东西,这样的页面模式是内容项的列表,让您的视图迭代的内容项目,并将其显示
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视图名。
所以主视图然后呈现了一些parital意见。最简单的部分视图只包含@Html.Raw(content.Body),其他人使用Content类的属性渲染更多的结构化内容:我有一个用于托管图像,一个用于新闻文章等。
然后在你的后端可以使用剑道控件(或其他)编辑ContentBody,ContentTeaser等,只设置一个适当的ContentType这名局部视图来渲染它。
希望这会给你足够的让你开始。
谢谢:) 我的问题,我有一个客户建立网站,现在的客户要求修改的网站,因为他喜欢,修改内容..Images ..等,以及所有视图通过剃刀 – Jala
唯一建你可以直接做的方式是让一些代码读取剃刀视图服务器端,并允许编辑这些代码 - 毕竟它们只是文件,并在运行时编译。但是,从非内容管理网站转到内容管理网站最好是通过引入CMS来完成。作为一个建议,Umbraco基于Razor,允许服务器端对模板进行编辑,对编辑者来说非常好用。但是,它的File:New-Project时间! – Andiih