Missing info tutorial, dropdownlist and ViewData

Dec 28, 2010 at 8:01 PM

I struggled to get passed page 82 in the pdf verison of tutorial because the dropdownlists were not showing in the "Edit" form until I added ViewData info in StoreManagerController.cs, in "Edit" ActionResult, like this:

public ActionResult Edit(int id)
        {
            var viewModel = new StoreManagerViewModel
            {
                Album = storeDB.Albums.Single(a => a.AlbumId == id),
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };


            ViewData["Artists"] = storeDB.Artists.ToList();
            ViewData["Genres"] = storeDB.Genres.ToList();


            return View(viewModel);
        }

 

I don't if this was the right thing to do or if there is a better way but it was not working for me without this addition.

 

Dec 29, 2010 at 1:45 AM
Edited Dec 29, 2010 at 1:47 AM
<%@ Import Namespace="MvcMusicStore"%>
  
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Album>" %>
  
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
  
Now we use the Html.EnableClientValidation helper method to "turn on" client-side validation. For the both the Create and Edit view templates, add that call directly above the Html.BeginForm call, like so:
<h2>Create</h2>
  
<% Html.EnableClientValidation(); %>
  
<% using (Html.BeginForm()) {%>
  
<fieldset>
    <legend>Create Album</legend>
    <%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres })%>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

 

plz check these codes. 

I don't know how it works. the list transfers from the lambda expression as the param in EditFor to the ViewData. However, it works well.