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());
}
}
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?
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*/
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
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;
which I'm using in my controller like this
And using that model in the view like this;
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!
try this:
https://our.umbraco.org/documentation/reference/routing/custom-controllers
Thanks for the quick reply!
I tried this but i get the following error;
Ah, Sorted!
It turns out I just had to change my Layout file's @inherit statement to this;
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.
And its HireItemViewModel class no longer needs to inherit the RenderModel
is working on a reply...