Umbraco中的Template默认都继承自 Umbraco.Web.Mvc.UmbracoTemplatePage

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

它使用了默认的model => Umbraco.Web.Models.RenderModel.

但是,如果你的这个template需要使用一个Custom model, 怎么办。

方法是,使用如下继承指令

@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyCustomModel>

但是,我们通常的做法是项目中有一个Layout模板(Layout template),项目中其他所有的模板(template)都继承自这个layout模板。 那么如果Layout模板使用的是继承自UmbracoTemplatePage (@inherits Umbraco.Web.Mvc.UmbracoTemplatePage)

,Layout模板默认的model就是RenderModel. 这种情况就会产生冲突,导致exception发生。因为我们的template继承自layout模板,但使用的确是MyCustomModle (@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyCustomModel>)

解决这个问题,有两种方法

方法一:

改变Layout模板中的继承,现在Layout模板是@inherits Umbraco.Web.Mvc.UmbracoTemplatePage,改为继承自 Umbraco.Web.Mvc.UmbracoViewPage<dynamic>,这意味着对应的Model将会是dynamic这个类型,而不再是Umbraco.Web.Models.RenderModel

@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>

这样的话,我们的template继承自layout模板,并且使用@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyCustomModel>, 就没有问题了

方法二

不改变Layout模板中的模板继承类型,也就是说还是@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

但是改变我们模板中使用的MyCustomModel,让它继承自Umbraco.Web.Models.RenderModel

public class MyCustomModel : RenderModel
{
//Standard Model Pass Through
public MyCustomModel(IPublishedContent content) : base(content) { } //Custom properties here...
public string MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
}

具体可参考 https://our.umbraco.org/documentation/Reference/Routing/custom-controllers

最新文章

  1. angularjs 指令详解 - template, restrict, replace
  2. 手动创建Spring项目 Spring framework
  3. isset和empty比较
  4. VC远控(二)连接Server端及密码验证
  5. bzoj 1041: [HAOI2008]圆上的整点 数学
  6. shell自定义函数
  7. ecshop获取url_domain
  8. 航频之声APP截图
  9. 【js编程艺术】小制作五
  10. linux 常用命令之一
  11. Android性能测试——Allocation Tracker(Device Monitor)
  12. 版本控制——TortoiseSVN (1)安装与配置
  13. CAS简介和无锁队列的实现
  14. [HNOI2014]世界树
  15. CTF杂项之BubbleBabble加密算法
  16. Android Getting Started
  17. python glob的安装和使用
  18. crontab 每分钟、每小时、每天、每周、每月、每年执行
  19. web@css引入方式,基本选择器,3大特性,高效运行
  20. Ansible playbook 批量修改服务器密码 先普通后root用户

热门文章

  1. Spark Mllib源码分析
  2. OJ 之 FATE
  3. 吴恩达机器学习笔记(三) —— Regularization正则化
  4. 使用 sqoop 将mysql数据导入到hdfs(import)
  5. JVM中垃圾回收算法
  6. codeforces 707D:Persistent Bookcase
  7. codevs1217 借教室
  8. QQ.PC管家进程
  9. Codeforces 432D Prefixes and Suffixes:KMP + dp
  10. CheckStyle:unable to parse configuration stream - Element type &quot;message&quot; must be declared