Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 3
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 7th, 2012, 11:01 PM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>'.
  #2 (permalink)  
Old February 9th, 2012, 09:58 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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).
  #3 (permalink)  
Old February 10th, 2012, 04:21 PM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default It Worked

Thanks, your suggestion worked!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.