简体   繁体   English

在Umbraco中,如何将对象的通用列表绑定到我的视图?

[英]In Umbraco, how can I bind a generic list of Objects to my View?

Something strange is happening in my umbraco project where I have a repository set up like so; 在我的umbraco项目中,发生了一些奇怪的事情,在该项目中,我建立了这样的存储库;

public class HireItemsRepo:BaseGenericRepository<YouHireItContext,HireItem>
{
   public List<HireItemViewModel> PopulateHireItemViewModel(RenderModel model)
    { List<HireItemViewModel> HireItems = new List<HireItemViewModel>();   
        foreach (var Hireitem in base.GetAll())
        {
            HireItems.Add(
              new HireItemViewModel(model.Content)
              {
                  Title = Hireitem.Title,
                  Price = Hireitem.Price
              }
           );
        }
        return HireItems;
    }

}

which I'm using in my controller like this 我在控制器中这样使用

 public class HiresController : RenderMvcController
    {
        // GET: Hire
        public override ActionResult Index(RenderModel model)
        {

            HireItemsRepo repo = new HireItemsRepo();
            var VM = repo.PopulateHireItemViewModel(model);

                return View("Hires",VM.ToList());
        }
    }

And using that model in the view like this; 并在这样的视图中使用该模型;

    @model List<You_Hire_It.Models.HireItemViewModel>

   /*HTML starts here*/

It's strange because if I try to use that model as a List, Umbraco will blow up with the following error; 奇怪的是,如果我尝试将该模型用作列表,Umbraco会因以下错误而崩溃;

Cannot bind source type System.Collections.Generic.List`1[[You_Hire_It.Models.HireItemViewModel, You_Hire_It, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to model type Umbraco.Web.Models.RenderModel. 无法将源类型System.Collections.Generic.List`1 [[You_Hire_It.Models.HireItemViewModel,You_Hire_It,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]绑定到模型Umbraco.Web.Models.RenderModel。

However, if I refactor all the code to use the model on it's own as if I only have one set of values to use, it has no problem with it! 但是,如果我重构所有代码以独自使用模型,就好像我只有一组要使用的值,那么它就没有问题!

Could anybody point me in the right direction with this please? 有人可以为此指出正确的方向吗?

Many thanks in advance! 提前谢谢了!

That is because the model you return from the action need to be of type RenderModel or inherit from it and in your case you are returning a List . 这是因为从操作返回的模型必须是RenderModel类型或从其继承,并且在这种情况下,您将返回List So your model should look something like this: 因此,您的模型应如下所示:

public class ViewModel : RenderModel
{
    public ViewModel(IPublishedContent content) : base(content) { }

    public List<HireItem> HireItems { get; set; }
}

public override ActionResult Index(RenderModel model)
{
    var vm = new ViewModel(model);
    vm.HireItems = new HireItemsRepo().GetHireItems();
    return View("Hires", vm);
}

You can inherit from RenderModel as DZL suggests. 您可以按照DZL的建议从RenderModel继承。 However, I generally prefer to use route hijacking which would enable me to keep my models simple. 但是,我通常更喜欢使用路由劫持,这可以使我的模型保持简单。

Instead of the Index method in your RenderMvcController, you can create a method with the same name as your view. 可以在您的RenderMvcController中代替Index方法,而可以创建一个与您的视图同名的方法。 I note your view is called Hires . 我注意到您的观点称为“ Hires So change your controller code to this: 因此,将您的控制器代码更改为:

public class HiresController : RenderMvcController
{
    // GET: Hire
    public ActionResult Hires(RenderModel model)
    {
        HireItemsRepo repo = new HireItemsRepo();
        var VM = repo.PopulateHireItemViewModel(model);

        return CurrentTemplate(VM)
    }
}

You now need to have your view inherit from UmbracoViewPage . 现在,您需要从UmbracoViewPage继承视图。 So at the top of your view replace the @model line with the following: 因此,在视图顶部,将@model行替换为以下内容:

@inherits UmbracoViewPage<List<HireItemViewModel>>

Your model in the view is now of type List<HireItemViewModel> which I think is what you want. 视图中的模型现在为List<HireItemViewModel>类型,我认为这是您想要的。

So to iterate the items you would use: 因此,要迭代使用的项目:

@foreach(var item in Model){
{
    // etc
}

Additionally, as this view now inherits from UmbracoViewPage , you have access to the UmbracoContext - just use @Umbraco 此外,由于此视图现在继承自UmbracoViewPage ,因此您可以访问UmbracoContext只需使用@Umbraco

For example: 例如:

@Umbraco.TypedContentAtRoot().Where(x=>x.DocumentTypeAlias == "HomePage")

or 要么

@Umbraco.AssignedContentItem etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM