An ASP.NET MVC 3 application has a problem filling dropdown boxes.

.net asp.net-mvc-3 c# drop-down-menu entity-framework

Question

I've finished the new MVC 3 lesson (musicstore) over at www.asp.net. Everything went well except from the portion where two dropdown boxes that should have been filled from the database weren't.

I adhered to the instructions and verified my code twice. I believe that utilizing the editorstemplate folder could be the issue. I have no clue as I am new to MVC. What exactly is the issue, and how can I fix it?

==============

Edit 1

Okay, so the code for album.cshtml, which is located in the /views/shared/editortemplates/ folder, is shown below.

   @model MvcMusicStore.Models.Album
<p> @Html.LabelFor(model => model.Genre) @Html.DropDownList("GenreId",
new SelectList(ViewBag.Genres as System.Collections.IEnumerable,
"GenreId", "Name", Model.GenreId))
</p>
<p> @Html.LabelFor(model => model.Artist) @Html.DropDownList("ArtistId",
new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))
</p>

This, in my opinion, includes:

public ActionResult Edit(int id)
{ ViewBag.Genres = storeDB.Genres.OrderBy(g => g.Name).ToList(); ViewBag.Artists = storeDB.Artists.OrderBy(a => a.Name).ToList();
var album = storeDB.Albums.Single(a => a.AlbumId == id);
return View(album);
}

Apart from the fact that the dropdowns are empty, I don't get any issues.

==============

Edit 2

I have album.cshtml in the /views/shared/editortemplates/album.cshtml folder and edit.cshtml in the /views/storemanager/edit.cshtml folder. Album.cshtml is designed to fill the dropdowns in edit.cshtml. It works perfectly when I immediately paste the code from album.cshtml into edit.cshtml. I believe the editortemplates/album.cshtml, which is responsible for populating the edit.cshtml page, is the cause of the issue. What gives, then? Thanks...

==============

Edit 3

Okay, I've located the issue and have the correct source code from CodePlex. It seems that my setup for the create.cshtml and edit.cshtml pages was incorrect. But everything is now repaired, so thanks.

1
2
2/6/2011 6:30:55 PM

Popular Answer

Working with view models is something I would advise against doing, and utilizing anyViewBag . Therefore, you begin by creating a view model:

public class AlbumViewModel
{
    public string GenreId { get; set; }
    public IEnumerable<Genre> Genres { get; set; }

    public string ArtistId { get; set; }
    public IEnumerable<Artist> Artists { get; set; }

    public Album Album { get; set; }
}

then after that, you would fill this view model inside your controller action:

public ActionResult Edit(int id)
{
    var model = new AlbumViewModel
    {
        Genres = storeDB.Genres.OrderBy(g => g.Name),
        Artists = storeDB.Artists.OrderBy(a => a.Name),
        Album = storeDB.Albums.Single(a => a.AlbumId == id)
    };
    return View(model);
}

lastly, in your editor template~/Views/Shared/EditorTemplates/AlbumViewModel.cshtml ):

@model MvcMusicStore.Models.AlbumViewModel
<p> 
    @Html.LabelFor(model => model.GenreId) 
    @Html.DropDownListFor(x => x.GenreId, new SelectList(Model.Genres, "GenreId", "Name"))
</p>

<p> 
    @Html.LabelFor(model => model.ArtistId) 
    @Html.DropDownListFor(x => x.ArtistId, new SelectList(Model.Artists, "ArtistId", "Name"))
</p>
13
2/2/2011 7:37:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow