2012-05-17 58 views
5

我刚开始做MVC 4,我一直很享受它,但是当我遇到麻烦时我会遇到麻烦试图让我的数据库(只是NAME和EMAIL条目)在索引视图中显示其所有条目。我得到以下错误:MVC - 词典需要一个'System.Collections.Generic.IEnumerable`类型的模型项目1

The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[MobileWebFormver2.Models.WebForm1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[MobileWebFormver2.Models.User]'.

我能够将程序连接到数据库,但我几乎卡在这里。我想知道我能否得到一些帮助。这里是我的代码:

用户类别

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 

namespace MobileWebFormver2.Models 
{ 
    public class User 
    { 
     [Required(ErrorMessage="Please enter name.")] 
     public string Name { get; set; } 
     [Required(ErrorMessage="Please enter email.")] 
     public string Email { get; set; } 
    } 
} 

的HomeController(WebForm1的是数据库条目,包含姓名和电子邮件字段)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MobileWebFormver2.Models; 
using System.Data.Entity; 

namespace MobileWebFormver2.Controllers 
{ 
    public class HomeController : Controller 
    { 
     DataClasses1DataContext db = new DataClasses1DataContext(); 

     public ActionResult Index() 
     { 
      ViewBag.Message = "This is the TourEast Holidays Mobile Index"; 

      return View(db.WebForm1s.ToList()); 
     } 
    } 
} 

指数

@model IEnumerable<MobileWebFormver2.Models.User> 

@{ 
    ViewBag.Title = "Home Page"; 
} 

@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
     @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
     @Html.DisplayFor(modelItem => item.Email) 
     </td> 
    </tr> 
} 

编辑:这是指DataClasses1DataContext。我不知道这将是多么有用。

DataClasses1.cs

namespace MobileWebFormver2.Models 
{ 
    partial class DataClasses1DataContext 
    { 
    } 
} 

DataClasses1.designer.cs

#pragma warning disable 1591 
//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.269 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace MobileWebFormver2.Models 
{ 
using System.Data.Linq; 
using System.Data.Linq.Mapping; 
using System.Data; 
using System.Collections.Generic; 
using System.Reflection; 
using System.Linq; 
using System.Linq.Expressions; 
using System.ComponentModel; 
using System; 


[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MobileWebForm")] 
public partial class DataClasses1DataContext : System.Data.Linq.DataContext 
{ 

    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); 

#region Extensibility Method Definitions 
partial void OnCreated(); 
partial void InsertWebForm1(WebForm1 instance); 
partial void UpdateWebForm1(WebForm1 instance); 
partial void DeleteWebForm1(WebForm1 instance); 
#endregion 

    public DataClasses1DataContext() : 
      base(global::System.Configuration.ConfigurationManager.ConnectionStrings["MobileWebFormConnectionString"].ConnectionString, mappingSource) 
    { 
     OnCreated(); 
    } 

    public DataClasses1DataContext(string connection) : 
      base(connection, mappingSource) 
    { 
     OnCreated(); 
    } 

    public DataClasses1DataContext(System.Data.IDbConnection connection) : 
      base(connection, mappingSource) 
    { 
     OnCreated(); 
    } 

    public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
      base(connection, mappingSource) 
    { 
     OnCreated(); 
    } 

    public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
      base(connection, mappingSource) 
    { 
     OnCreated(); 
    } 

    public System.Data.Linq.Table<WebForm1> WebForm1s 
    { 
     get 
     { 
      return this.GetTable<WebForm1>(); 
     } 
    } 
} 

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.WebForm1")] 
public partial class WebForm1 : INotifyPropertyChanging, INotifyPropertyChanged 
{ 

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

    private string _Name; 

    private string _Email; 

#region Extensibility Method Definitions 
partial void OnLoaded(); 
partial void OnValidate(System.Data.Linq.ChangeAction action); 
partial void OnCreated(); 
partial void OnNameChanging(string value); 
partial void OnNameChanged(); 
partial void OnEmailChanging(string value); 
partial void OnEmailChanged(); 
#endregion 

    public WebForm1() 
    { 
     OnCreated(); 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="NVarChar(50) NOT NULL", CanBeNull=false, IsPrimaryKey=true)] 
    public string Name 
    { 
     get 
     { 
      return this._Name; 
     } 
     set 
     { 
      if ((this._Name != value)) 
      { 
       this.OnNameChanging(value); 
       this.SendPropertyChanging(); 
       this._Name = value; 
       this.SendPropertyChanged("Name"); 
       this.OnNameChanged(); 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Email", DbType="NVarChar(50) NOT NULL", CanBeNull=false)] 
    public string Email 
    { 
     get 
     { 
      return this._Email; 
     } 
     set 
     { 
      if ((this._Email != value)) 
      { 
       this.OnEmailChanging(value); 
       this.SendPropertyChanging(); 
       this._Email = value; 
       this.SendPropertyChanged("Email"); 
       this.OnEmailChanged(); 
      } 
     } 
    } 

    public event PropertyChangingEventHandler PropertyChanging; 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void SendPropertyChanging() 
    { 
     if ((this.PropertyChanging != null)) 
     { 
      this.PropertyChanging(this, emptyChangingEventArgs); 
     } 
    } 

    protected virtual void SendPropertyChanged(String propertyName) 
    { 
     if ((this.PropertyChanged != null)) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
} 
#pragma warning restore 1591' 

我看着类似的帖子在这里,我知道它有传递对象的事,但观点期待别的东西。不过,我会说我是一名初学者水平的程序员,我不认为我理解他们所说的大部分内容。我基本上遵循了来自Microsoft的MVC教程网站(Link)中关于如何在索引中显示数据库的代码,但是我收到了错误消息。任何指导将不胜感激。

+0

你可以显示你的DbContext的实现吗?你需要一个DbSet 来进行查询。 –

+0

您能否向我提供关于如何显示实施的说明?我的道歉,我是一个真正的初学者哈哈。谢谢! –

+0

好吧,我去了我的模型,并有一个DataClasses1.dbml。我右键单击它并单击查看代码,现在我在DataClasses1.cs中。在代码中的所有存在的是:命名空间MobileWebFormver2.Models { 部分类DataClasses1DataContext { } } @LeonCullens –

回答

6

问题是,你错误的东西传入视图。您正在传递WebForm1的集合,而不是User的集合。

编辑 - 您的视图模型改成这样:

@model IEnumerable<MobileWebFormver2.Models.WebForm1> 

编辑 - 详细说明一下:

在此行中,你所创建的的ActionResult和数据传递的模式。

return View(db.WebForm1s.ToList()); 

但是,db.WebForm1s是WebForm1类型的集合。

在你看来,你是一个强类型的视图,当你声明型号:

@model IEnumerable<MobileWebFormver2.Models.User> 

视图预计用户的集合,而是传递WebForm1的替代。因此,错误 - 您必须决定哪一个是正确的,控制器传入WebForm1或视图,并更改另一个以匹配。

+0

假设DataClasses1DataContext包含理所当然的DbSet 称为用户。 –

+0

的确,我只是假设他没有展示他的数据库上下文,但似乎是一个合理的假设。 – Leniency

+0

请问如何让DataClasses1DataContext包含DbSet ?当我尝试添加db.Users.ToList()时,它没有编译。如果这是一个愚蠢的问题,我很抱歉,我对编程特别是面向对象的新手很陌生。我真的很感谢帮助:) –

相关问题