Umbraco 的template使用的默认Model问题
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
最新文章
- angularjs 指令详解 - template, restrict, replace
- 手动创建Spring项目 Spring framework
- isset和empty比较
- VC远控(二)连接Server端及密码验证
- bzoj 1041: [HAOI2008]圆上的整点 数学
- shell自定义函数
- ecshop获取url_domain
- 航频之声APP截图
- 【js编程艺术】小制作五
- linux 常用命令之一
- Android性能测试——Allocation Tracker(Device Monitor)
- 版本控制——TortoiseSVN (1)安装与配置
- CAS简介和无锁队列的实现
- [HNOI2014]世界树
- CTF杂项之BubbleBabble加密算法
- Android Getting Started
- python glob的安装和使用
- crontab 每分钟、每小时、每天、每周、每月、每年执行
- web@css引入方式,基本选择器,3大特性,高效运行
- Ansible playbook 批量修改服务器密码 先普通后root用户
热门文章
- Spark Mllib源码分析
- OJ 之 FATE
- 吴恩达机器学习笔记(三) —— Regularization正则化
- 使用 sqoop 将mysql数据导入到hdfs(import)
- JVM中垃圾回收算法
- codeforces 707D:Persistent Bookcase
- codevs1217 借教室
- QQ.PC管家进程
- Codeforces 432D Prefixes and Suffixes:KMP + dp
- CheckStyle:unable to parse configuration stream - Element type ";message"; must be declared