Edit View is empty

Jul 21, 2010 at 5:56 AM

Hi,

 

I am on page 53 of tutorial v 0.8, I can't get my Edit view to appear when I click on Edit link. I've checked everything before and after page 53 that relates to editing album information, but no luck. Do you know what I may be missing?

My Edit view has Edit album group and a save button and that's it.

When I go with debug I know it goes to album.ascx and passess through the code of label and text field creation...

 

I've created Album.ascx:

<%@ Import Namespace="MvcMusicStore" %>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Album>" %>



<p>
    <%: Html.LabelFor(model => model.Title) %>
    <%: Html.TextBoxFor(model => model.Title) %>
    <%: Html.ValidationMessageFor(model => model.Title) %>
</p>          
<p>
    <%: Html.LabelFor(model => model.Price) %>
    <%: Html.TextBoxFor(model => model.Price) %>
    <%: Html.ValidationMessageFor(model => model.Price) %>
</p>         
<p>
    <%: Html.LabelFor(model => model.AlbumArtUrl) %>
    <%: Html.TextBoxFor(model => model.AlbumArtUrl) %>
    <%: Html.ValidationMessageFor(model => model.AlbumArtUrl) %>
</p>
<p>
    <%: Html.LabelFor(model => model.Artist) %>
    <%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artist"] as IEnumerable,"ArtistId", "Name", Model.ArtistId)) %>
</p>

<p>
    <%: Html.LabelFor(model => model.Genre) %>
    <%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable,
    "GenreId", "Name", Model.GenreId)) %>
</p>
            

My StoreManagerController looks like that:
  //
        // GET: /StoreManager/Edit/5
 
        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);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var album = storeDB.Albums.Single(a => a.AlbumId == id);

            try
            {
                //Save album

                UpdateModel(album, "Album");
                storeDB.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    Album = album,
                    Genres = storeDB.Genres.ToList(),
                    Artists = storeDB.Artists.ToList()
                };

                return View(viewModel);
            }
        }
And I've created Edit view (in StoreManager folder):
<%@ 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</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        
        <fieldset>
            <legend>Edit Album</legend>
                  <% Html.EditorFor(model => model.Album,
                   new { Artist = Model.Artists, Genres = Model.Genres }); %> 
<%--            <% Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable,
                 "ArtistId", "Name", Model.ArtistId %>--%>
            <p>

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



    <% } %>

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

</asp:Content>

Jul 21, 2010 at 11:32 PM

I did tutorial again and it seems to work now. But I couldn't figure out what is missing in this project though...

Jul 22, 2010 at 7:30 AM
Edited Jul 22, 2010 at 7:32 AM

<% Html.EditorFor(model => model.Album,
                   new { Artist = Model.Artists, Genres = Model.Genres }); %>
I think that Html functions returns a string. so this sentense should be  changed to:

<%: Html.EditorFor(model => model.Album,
                   new { Artist = Model.Artists, Genres = Model.Genres }) %>

or like this in VS2008:

<%=Html.EditorFor(model => model.Album,
                   new { Artist = Model.Artists, Genres = Model.Genres }) %>

Coordinator
Jul 26, 2010 at 5:16 AM

That's correct, Kate - you need to use <%: when you're using the Html.EditorFor helpers, since they return a string.