Rusty
Rusty

Reputation: 1303

MVC creating a view issue

   public ActionResult AlbumList3(
   {
   AlbumService albumservice = new AlbumService();
   var PagingViewModel = new PagingViewModel<Album> { Query = albumservice.GetAlbums(),  ViewData = ViewData };
   return View(PagingViewModel);
   }

I am trying to create view for the above coding. And MY pagingviewmodel class is generic class PagingViewModel<T>, which I cannot select Viewdataclass drop down while making view.

So when i create a strongly typed => empty view then i cannot get values of the entity.

Gives below error:

Cannot implicitly convert type 'MvcApplication1.Models.PagingViewModel' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Thanks

Upvotes: 0

Views: 70

Answers (1)

jao
jao

Reputation: 18620

On the first line of your view add:

@model PagingViewModel<Album>

you can then do:

@foreach(var item in Model.Query)

to loop through the albums.

Update

In the case of ASPX views, change:

Inherits="System.Web.Mvc.ViewPage"

To

Inherits="System.Web.Mvc.ViewPage<PageViewModel<Album>>"

And to access the items:

<% foreach(var item in Model.Query) { %>

Upvotes: 1

Related Questions