2010-10-20 73 views
0

的定义我目前正在通过MVC音乐商店教程。目前我被困在第53页,我想知道是否有人可以帮助我。MVC错误:'对象'不包含''

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

'object' does not contain a definition for 'Artists'

'object' does not contain a definition for 'Genres'

我想我看着它的时间太长了,我不能当场我的错误。一双新鲜的眼睛可以做到这一点!

这里是发生错误的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,我把它称为错误,但一切都相应引用。

回答

3

替换:

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

有了:

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

换句话说,你要创建一个新的匿名类型。上面的语法实际上试图创建一个新的裸露的object并将值分配给object上不存在的属性:“艺术家”和“流派”。

+1

+1 - 这个错误通常是由intellisense引起的,当你在单词“new”后点击空格时,intellisense会粘住单词“object”。唯一已知的解决方法是1)记得删除它,2)输入“new {”,没有空格(当你得到关闭时它会增加空间)“}”。 – Fenton 2010-10-20 14:51:59

+0

谢谢!这是智能感觉好吧我想我正在寻找更复杂的东西!一旦时间到了,我会把它标记为正确的答案! – 109221793 2010-10-20 14:57:36

+0

@索尼,这已经是我一生中很长一段时间的祸根! – DaveDev 2010-10-29 23:35:29