MVCMusicStore 1.0 tutorial, help please..stuck here. Edit view doesn't show the dropdownlist control

Dec 26, 2010 at 7:45 PM
Edited Dec 26, 2010 at 7:46 PM

Hello guys, first of all 'Seasons greetings to you'.

I have been following the tutorial and it was going great until I got stuck on this one. 

When I goto

../StoreManager/Edit/400

I see the form with an album details but I donot see the dropdownlist. Heck..I even see Genre Id, and Albumid. For some reason, Album.ascx is not being used. 

This is how it is looking screenshot (opens new window).

 

 

//---StoreManagerController.cs-----------------

 

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()
            };

            return View(viewModel);
        }
--------------------------------------------


//-StoreManagerViewModel.cs--------------
using
System.Collections.Generic; using MvcMusicStore.Models; namespace MvcMusicStore.ViewModels { public class StoreManagerViewModel { public Album Album { get; set; } public List<Artist> Artists { get; set; } public List<Genre> Genres { get; set; } } }

 

--------------------------------------------------------------------

 

--Album.ascx----------------------------------------------------

 

<%@ 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>

<div class="editor-label">
    <%: Html.LabelFor(model => model.GenreId) %>
</div>
<div class="editor-field">
    <%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%>
    <%: Html.ValidationMessageFor(model => model.GenreId) %>
</div>
            
<div class="editor-label">
    <%: Html.LabelFor(model => model.ArtistId) %>
</div>
<div class="editor-field">
    <%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%>
    <%: Html.ValidationMessageFor(model => model.ArtistId) %>
</div>
            
<div class="editor-label">
    <%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Title) %>
    <%: Html.ValidationMessageFor(model => model.Title) %>
</div>
            
<div class="editor-label">
    <%: Html.LabelFor(model => model.Price) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>
    <%: Html.ValidationMessageFor(model => model.Price) %>
</div>
            
<div class="editor-label">
    <%: Html.LabelFor(model => model.AlbumArtUrl) %>
</div>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.AlbumArtUrl) %>
    <%: Html.ValidationMessageFor(model => model.AlbumArtUrl) %>
</div>

 

--------------------------------------------------------------------------

 

//-Edit.aspx--------------------------------------------------------------

 

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.StoreManagerViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Edit - <%: Model.Album.Title %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit Album</h2>

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        
    <fieldset>
        <legend>Edit Album</legend>
        <%: Html.EditorFor(model => model.Album, 
            new { Artists = Model.Artists, Genres = Model.Genres}) %>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

-------------------------------------------------------------------------------------

 

Dec 26, 2010 at 9:06 PM

Fixed. What a dummy I am.

I had the album.ascx stored in "../Shared" folder. But it was supposed to be in "../Shared/EditorTemplates" folder. After I created the 'EditorTemplates' and moved 'Album.ascx' into it, everything became normal. Now I can move on. :)

Coordinator
Dec 26, 2010 at 10:29 PM

Yes, that's it - your Editor templates need to be in /Shared/EditorTemplates, and Display templates need to be in /Shared/DisplayTemplates. Glad you got it figured out!