开发者

mvc 3 musicstore - storemanagercontroller - pagination

This post is in regards to MVC MusicStore, MVC 3, which is freely available from MSDN website I am not sure if this is a problem in cshtml file or in the cs file. Basically I am trying to implement pagination for the StoreManagerController. I had a looked http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/,http://weblogs.asp.net/rajbk/archive/2010/05/08/asp-net-mvc-paging-sorting-filtering-using-the-mvccontrib-grid-and-pager.aspx and http://weblogs.asp.net/shijuvarghese/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx I am not getting the end result right. Could someone kindly help me? I think in its something to do with class in storemanagercontroller, or perhaps I need to to create antother class, or maybe in @model IEnumerable bearing in mind I have Helper.cs file as well?

The output should be...

Artist - For Those About To Rock We Salute You

Title - AC/DC

Genre - Rock

etc

then the pagination shows at the bottom of the screen 1 2 3 4 5 >

But I am getting an output...

Artist - MvcMusicStore.Models.Artist

Title - For Those About To Rock We Salute You

Genre - MvcMusicStore.Models.Genre

etc

then the pagination shows at the bottom of the screen 1 2 3 4 5 >

StoreManagerController syntax is

                        public ActionResult Index() 
                          {
                           var albums = storeDB.Albums
                           .Include("Genre").Include("Artist")
                           .ToList();
                           return View(albums);
                          }

In the cshtml (StoreManagerController -> view->Album class as strongly typed)I have implemented the following code...

              @model IEnumerable<MvcMusicStore.Models.Album>
              @{
                 ViewBag.Title = "Index";
               }
              <h2>Index</h2>
              @Html.ActionLink("Create New", "Create")
              @{        
                 var grid = new WebGrid(source: Model,
                 defaultSort: "Artist",
                 rowsPerPage: 10);
               }

             <div id="grid">
                @grid.GetHtml(
                tab开发者_Python百科leStyle: "grid",
                headerStyle: "head",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
                grid.Column("Artist"),
                grid.Column("Title"),
                grid.Column("Genre")
               )

             ); // ; was missing. 


The problem is that Genre and Artist are entities. You can create a ViewModel like this:

public class AlbumViewModel
    {        
        public string Artist { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
    }

and than fill it inside controller like this:

var albums = from a in storeDB.Albums
             select new AlbumViewModel{
                        Artist = a.Artist.Name, 
                        Title = a.Title, 
                        Genre = a.Genre.Name };

and don't forget to set the model type in a view:

@model IEnumerable<MvcMusicStore.Models.AlbumViewModel>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜