路由 hijacking/Cannot 将源类型 Umbraco.Web.Models.RenderModel 绑定到模型类型
route hijacking/Cannot bind source type Umbraco.Web.Models.RenderModel to model type
我正在尝试在 umbraco 中创建一个强类型视图..但我卡在了一个点上,我收到了这个错误。
Cannot bind source type Umbraco.Web.Models.RenderModel to model type
umbraco_demo.Model.HomeModel.
我的模型class:
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);
}
}
在 umbraco 中查看:
@using umbraco_demo.Model
@inherits UmbracoViewPage<HomeModel>
@{
Layout = "Master.cshtml";
}
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
我在 umbraco 中还有一个文档类型,名称为 Home,上面有模板。
我什至提到了这个 post on umbraco forum
但仍然出现同样的错误。
修复控制器
将您的控制器方法名称更改为:
public override ActionResult Index(RenderModel model)
{
...
}
Umbraco.Web.Mvc.RenderMvcController
有一个方法也称为 Index
,您需要覆盖它。否则它可能会使用该虚拟方法作为默认方法(这将 return 不同的模型类型)。
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;}
还要确保您的控制器名称与您当前使用的文档类型相同
我正在尝试在 umbraco 中创建一个强类型视图..但我卡在了一个点上,我收到了这个错误。
Cannot bind source type Umbraco.Web.Models.RenderModel to model type umbraco_demo.Model.HomeModel.
我的模型class:
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);
}
}
在 umbraco 中查看:
@using umbraco_demo.Model
@inherits UmbracoViewPage<HomeModel>
@{
Layout = "Master.cshtml";
}
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
我在 umbraco 中还有一个文档类型,名称为 Home,上面有模板。
我什至提到了这个 post on umbraco forum 但仍然出现同样的错误。
修复控制器
将您的控制器方法名称更改为:
public override ActionResult Index(RenderModel model)
{
...
}
Umbraco.Web.Mvc.RenderMvcController
有一个方法也称为 Index
,您需要覆盖它。否则它可能会使用该虚拟方法作为默认方法(这将 return 不同的模型类型)。
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;}
还要确保您的控制器名称与您当前使用的文档类型相同