开发者

How does the MvcMusicStore tutorial get Edit Template views work?

I'm in part four of the MvcMusicStore tutorial.

I just finished creating my Edit template view and putting it in the Views/Shared folder. The problem is I can't figure out how to make it work.

My controller action is:

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

My shared edit view is (Album.ascx):

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

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>
    
    <fieldset>
        <legend>Fields</legend>
        
        <div class="editor-label">
            <%: Html.LabelFor(model => model.AlbumId) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.AlbumId) %>
            <%: Html.ValidationMessageFor(model => model.AlbumId) %>
开发者_如何学Go        </div>
        
        <div class="editor-label">
            <%: Html.LabelFor(model => model.GenreId) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.GenreId) %>
            <%: Html.ValidationMessageFor(model => model.GenreId) %>
        </div>
        
        <div class="editor-label">
            <%: Html.LabelFor(model => model.ArtistId) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => 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>
        
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

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

And my album edit view is:

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</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 { Artists = Model.Artists, Genres = Model.Genres}) %>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

<% } %>

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

</asp:Content>

When I run /StoreManager/Edit/386 I get this error:

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web >request. Please review the stack trace for more information about the error and where it >originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of >an object.

Source Error:

Line 14: Line 15: Edit Album Line 16: <%: Html.EditorFor(model => model.Album, Line 17: new { Artists = Model.Artists, Genres = Model.Genres}) %> Line 18:

I went through the projects completed source code and as far as I can tell my source is the same as theirs. Any ideas as to what could be causing this?


Yeah... just needed 5 more minutes before turning to StackOverflow to figure out the issue. My controller should have returned return View(viewModel); but instead I initially had it return View();.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜