Umbraco自定义ViewModel

时间:2019-06-06 18:44:57

标签: umbraco

我想将数据传递给视图,我创建了一个从RenderModel继承的viewmodel,但是当我遇到错误“ Element”时,Umbraco.Web.Models.RenderModel不包含定义“ Topic”

ViewModel:

namespace Umbraco12.Models
{
    public class Home : RenderModel
    {
        public Home(IPublishedContent content, CultureInfo culture) : base(content, culture)
        {
        }

        public string Topic { get; set; }
    }
}

控制器:

  public class HomeController : Umbraco.Web.Mvc.RenderMvcController
    {
        // GET: Home
        public ActionResult Home(RenderModel model)
        {

            var home = new Home(model.Content, model.CurrentCulture);
            home.Topic = "aloha";
            //Do some stuff here, then return the base method
            return View("Home", home);
        }
    }

查看:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Home>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
    Layout = "Master.cshtml";
}
<h1>@Umbraco.Field("topic") : @Model.Topic</h1>

1 个答案:

答案 0 :(得分:0)

确保您的DocumentType别名为“主页”。

我还会将您的模型称为不同于Home的模型,因为它可能发生冲突并产生模棱两可的错误。

控制器:

  public class HomeController : Umbraco.Web.Mvc.RenderMvcController
    {
        // GET: Home
        public ActionResult Index(RenderModel model)
        {

            var home = new Home(model.Content, model.CurrentCulture);
            home.Topic = "aloha";
            //Do some stuff here, then return the base method
            return View("Home", home);
        }
    }

查看:

@inherits UmbracoViewPage<Umbraco12.Models.Home>

@{
    Layout = "Master.cshtml";
}
<h1>@Model.Topic</h1>