Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Reece Cottam 4 posts 73 karma points
    Jan 13, 2018 @ 18:36
    Reece Cottam
    0

    Why wont Umbraco let me 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;

    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;

    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.

    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!

  • Marcio Goularte 388 posts 1360 karma points
    Jan 13, 2018 @ 19:13
    Marcio Goularte
    0

    try this:

    public class HiresController : RenderMvcController
        {
            // GET: Hire
            public override ActionResult Index(RenderModel model)
            {
    
                HireItemsRepo repo = new HireItemsRepo();
                var VM = repo.PopulateHireItemViewModel(model);
    
                return base.Index(VM.ToList());    
    
            }
        }
    
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<List<You_Hire_It.Models.HireItemViewModel>>
    
     /*HTML starts here*/
    

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

  • Reece Cottam 4 posts 73 karma points
    Jan 13, 2018 @ 19:17
    Reece Cottam
    0

    Thanks for the quick reply!

    I tried this but i get the following error;

    Argument 1: cannot convert from 'System.Collections.Generic.List<Models.HireItemViewModel>' to 'Umbraco.Web.Models.RenderModel' 
    
  • Reece Cottam 4 posts 73 karma points
    Jan 13, 2018 @ 19:33
    Reece Cottam
    0

    Ah, Sorted!

    It turns out I just had to change my Layout file's @inherit statement to this;

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>
    
  • Marcio Goularte 388 posts 1360 karma points
    Jan 13, 2018 @ 19:36
    Marcio Goularte
    0

    Sorry, I just passed a wrong tip. You need to create a model class where the list is a property. Here is an example I make when I have a scenario like yours.

    public class MyNewViewModel : RenderModel
    {
    
        public MyNewViewModel(IPublishedContent content) : base(content) { }
    
        //Custom properties here...
        public List<You_Hire_It.Models.HireItemViewModel> Items { get; set; }
    
    }
    
    public class HiresController : RenderMvcController
        {
            // GET: Hire
            public override ActionResult Index(RenderModel model)
            {
    
                HireItemsRepo repo = new HireItemsRepo();
    
                var mynewviewmodel = MyNewViewModel(model.Content);
                mynewviewmodel.Items = repo.PopulateHireItemViewModel(model);
    
                return base.Index(mynewviewmodel);    
    
            }
        }
    
    
        @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyNewViewModel>
    
    
    
        @if (Model.Items != null && Model.Items.Any())
        {
          foreach (var item in Model.Items)
                   {
                   }
    
        }
    

    And its HireItemViewModel class no longer needs to inherit the RenderModel

Please Sign in or register to post replies

Write your reply to:

Draft