MVC错误:'object'不包含''的定义

时间:2010-10-20 14:45:38

标签: c# asp.net-mvc class

我目前正在通过MVC音乐商店教程。我现在被困在第53页,我想知道是否有人可以帮助我。

我目前收到以下两个错误:

  

'object'不包含'Artists'的定义

     

'object'不包含定义   为'流派'

我想我已经看了太久了,我无法发现错误。一双新鲜的眼睛可以做到这一点!

以下是发生错误的aspx文件:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMovies1.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 object{ Artists = Model.Artists, Genres = Model.Genres }) %>

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


    </fieldset>

      <% } %>

</asp:Content>

以下是此页面应该调用的类,可以这么说:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcMovies1.Models;

namespace MvcMovies1.ViewModels
{
    public class StoreManagerViewModel
    {
        public Album Album { get; set; }
        public List<Artist> Artists { get; set; }
        public List<Genre> Genres { get; set; }
    }
}

PS - 我意识到我的一些名字是MvcMovies1,我错误地称之为,但所有内容都被相应引用。

1 个答案:

答案 0 :(得分:3)

替换:

new object{ Artists = Model.Artists, Genres = Model.Genres } 

使用:

new { Artists = Model.Artists, Genres = Model.Genres }

换句话说,您想要创建一个新的匿名类型。上面的语法实际上尝试创建一个新的object,并为object上不存在的属性赋值:“艺术家”和“流派”。