Wrox Programmer Forums
|
BOOK: Professional ASP.NET MVC 3
This is the forum to discuss the Wrox book Professional ASP.NET MVC 3 by Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen; ISBN: 978-1-1180-7658-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 7th, 2012, 11:01 PM
Registered User
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5: Edit Album DropDownList

After some time trying get the example on Pg. 105 working, I'm crying Uncle!
In debug I can see the ViewBag.Genres populated with SelectListItems, but I can't figure out how to get them into a SelectList that DropDownList can use. Thanks, to anyone who might shed light on this.

My Controller code is (straight from the book)

Code:
// ~/Controllers/AlbumController.cs

{
  public class AlbumController : Controller
  {
    private MusicStoreDB storeDB = new MusicStoreDB();

    // 
    // GET: /Album/Edit/#    
    public ActionResult Edit(int id)
    {
      var album = storeDB.Albums.Single(a => a.AlbumId == id);

      // Page 105 - Use LINQ to project Generes into SelectListItem
      ViewBag.Genres = storeDB.Genres
        .OrderBy(g => g.Name)
        .AsEnumerable()
        .Select(g => new SelectListItem
        {
          Text = g.Name,
          Value = g.GenreId.ToString(),
          Selected = album.GenreId == g.GenreId
        });
      return View(album);
    }
  }
}
My View Code is ...

Code:
@* ~/Views/Album/Edit.cshtml  *@

@model MvcMusicStore.Models.Album
@using (Html.BeginForm())
{
  @Html.ValidationSummary(excludePropertyErrors: true)
  <fieldset>
    <legend>Edit Album</legend>
    <p>
      @Html.Label("GenreId")
      @Html.DropDownList("GenreId", ViewBag.Genres as SelectList)  
    </p>
    <p>    
      @Html.Label("Title")
      @Html.TextBox("Title", Model.Title)
  
    </p>
    <input type="submit" value="Save" />
  </fieldset>
}
The view throws an exception at @Html.DropDownList ..

Code:
The ViewData item that has the key 'GenreId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
 
Old February 9th, 2012, 09:58 AM
Authorized User
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Does

@Html.DropDownList("GenreId", (IEnumerable<SelectListItem>)ViewBag.Genres)


help?

(A bit blind leading the blind here.....I had something similar and it worked).
 
Old February 10th, 2012, 04:21 PM
Registered User
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default It Worked

Thanks, your suggestion worked!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14-Photo Album Pages Monochrome vs Dark Grey jlransford BOOK: Beginning ASP.NET 4 : in C# and VB 3 August 3rd, 2010 12:08 PM
Chapter 7, Online Photo Album, Ajax, cannot upload to subdirectories - here is the so kenj BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 0 March 31st, 2010 09:19 PM
Enable dropdownlist on Edit GS ASP.NET 2.0 Professional 2 March 19th, 2007 12:42 PM
edit dynamic dropdownlist in a datalist jacthemanus ASP.NET 1.0 and 1.1 Professional 1 September 28th, 2006 12:08 PM
How do I setup a dropdownlist for (*Type*) in edit macupryk General .NET 0 October 9th, 2004 07:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.