我正在尝试在umbraco中制作强类型视图。但我被困在出现此错误的地步。
无法将源类型 Umbraco.Web.Models.RenderModel 绑定到模型类型 umbraco_demo。模型.家模型.
我的模型类:
public class HomeModel : RenderModel
{
//Standard Model Pass Through
public HomeModel(IPublishedContent content) : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent, UmbracoContext.Current.PublishedContentRequest.Culture) { }
//Custom properties here...
public string MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
}
我的控制器
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult HomeModel(RenderModel model)
{
//we will create a custom model
var myCustomModel = new HomeModel(model.Content);
myCustomModel.MyProperty1 = DateTime.Today.ToString();
//TODO: assign some values to the custom model...
return CurrentTemplate(myCustomModel);
}
}
在本布拉科州查看:
@using umbraco_demo.Model
@inherits UmbracoViewPage<HomeModel>
@{
Layout = "Master.cshtml";
}
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
此外,我在umbraco中有一个文档类型,名称为Home,具有上述模板。
我什至在本布拉科论坛上提到了这篇文章 但仍然得到同样的错误。
修复控制器
将控制器方法名称更改为:
public override ActionResult Index(RenderModel model)
{
...
}
Umbraco.Web.Mvc.RenderMvcController
有一个也称为Index
的方法,您需要重写它。否则,它可能会使用该虚拟方法作为默认值(这将返回不同的模型类型)。
Index
也是当页面加载没有(GET 或 POST)参数时将调用的默认方法。
如果在控制器上使用调试器,则应该能够看到自定义方法在页面加载时未被命中。
更新模型
我还没有使用您对模型的特定实现。可能值得将构造函数更改为:
public HomeModel(IPublishedContent content, CultureInfo culture) : base (content, culture) { }
并将此模型的控制器中的实例化更改为:
var myCustomModel = new HomeModel(model.Content, System.Globalization.CultureInfo.CurrentCulture);
查看更改
您视图中试图获取属性MyProperty1
的行可能是错误的。我假设此属性在您的 Umbraco 节点上不存在,但您的意思是访问自定义模型上的属性。
改变:
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
自:
@{var myProperty1 = Model.MyProperty1;}
还要确保您的控制器名称与您当前正在使用的文档类型相同