在 Umbraco 中使用自定义模型 - 错误?
Using custom Model in Umbraco - error?
在 Umbraco 中使用自定义模型时出现以下错误:
Cannot bind source type Umbraco.Web.Models.RenderModel to model type MyParser.Model.MyModel.
包含此模型的代码是一个旧项目,我正在 'importing' 进入 Umbraco 站点 - 它曾经只是一个普通的 MVC 应用程序。
对于我的使用,它不需要与 Umbraco 或数据库交互 - 它只需要存在于同一个项目中。
调用模型的布局如下所示;
@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyParser.Model.MyModel>
@{
Layout = null;
}
模型看起来像这样
namespace MyParser.Model
{
public class MyModel
{
public string Name { get; set; }
public string Value1 { get; set; }
public bool IsValid { get; set; }
}
}
该代码在 vanilla MVC 应用程序中运行良好,但需要在 Umbraco 应用程序中修改为 运行 看起来 - 但如何?
它是 Umbraco 7.6
如果是文档类型模板,需要劫持路由注入视图模型。您创建继承自 RenderMvcController
的控制器并覆盖 Index
方法。
namespace Controllers
{
public class [YourDocTypeAlias]Controller : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var vm = new ViewModel<MyModel>(model);
return View(vm);
}
}
public class ViewModel<TModel> : RenderModel
{
public ViewModel(RenderModel model) : base(model.Content, model.CurrentCulture) { }
public TModel CustomModel { get; set; }
}
public class MyModel
{
public string Name { get; set; }
public string Value1 { get; set; }
public bool IsValid { get; set; }
}
}
你的模板现在变成了。
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ViewModel<MyModel>>
@{
Layout = "Master.cshtml";
//you can now access properties from MyModel like Model.CustomModel.IsValid...
}
可以在 link 上找到更多信息。
在 Umbraco 中使用自定义模型时出现以下错误:
Cannot bind source type Umbraco.Web.Models.RenderModel to model type MyParser.Model.MyModel.
包含此模型的代码是一个旧项目,我正在 'importing' 进入 Umbraco 站点 - 它曾经只是一个普通的 MVC 应用程序。
对于我的使用,它不需要与 Umbraco 或数据库交互 - 它只需要存在于同一个项目中。
调用模型的布局如下所示;
@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyParser.Model.MyModel>
@{
Layout = null;
}
模型看起来像这样
namespace MyParser.Model
{
public class MyModel
{
public string Name { get; set; }
public string Value1 { get; set; }
public bool IsValid { get; set; }
}
}
该代码在 vanilla MVC 应用程序中运行良好,但需要在 Umbraco 应用程序中修改为 运行 看起来 - 但如何? 它是 Umbraco 7.6
如果是文档类型模板,需要劫持路由注入视图模型。您创建继承自 RenderMvcController
的控制器并覆盖 Index
方法。
namespace Controllers
{
public class [YourDocTypeAlias]Controller : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var vm = new ViewModel<MyModel>(model);
return View(vm);
}
}
public class ViewModel<TModel> : RenderModel
{
public ViewModel(RenderModel model) : base(model.Content, model.CurrentCulture) { }
public TModel CustomModel { get; set; }
}
public class MyModel
{
public string Name { get; set; }
public string Value1 { get; set; }
public bool IsValid { get; set; }
}
}
你的模板现在变成了。
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ViewModel<MyModel>>
@{
Layout = "Master.cshtml";
//you can now access properties from MyModel like Model.CustomModel.IsValid...
}
可以在 link 上找到更多信息。