MVC - Dictionary需要一个类型为&System; Series.Collections.Generic.IEnumerable`1的模型项

时间:2012-05-17 00:52:29

标签: asp.net-mvc razor

我刚刚开始做MVC 4并且我一直很喜欢它,但是当我试图获取我的数据库(只是NAME和EMAIL条目)以显示其中的所有条目时,我遇到了麻烦索引视图。我收到以下错误:

  

传入字典的模型项类型为'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是一个数据库条目,包含NAME和EMAIL字段)

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'

我在这里看了类似的帖子,我知道它与传递一个对象有关,但是视图期待其他东西。但是,我会说我是一个非常初级的程序员,我不认为我理解他们所说的大部分内容。我基本上遵循了微软关于如何在索引中显示数据库的MVC教程站点(Link)中的代码,但是我收到了错误。任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:7)

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

编辑 - 将您的视图模型更改为:

@model IEnumerable<MobileWebFormver2.Models.WebForm1>

编辑 - 更多解释:

在这一行上,您将创建ActionResult并传入模型的数据。

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

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

在您的视图中,当您声明模型时,您正在创建强类型视图:

@model IEnumerable<MobileWebFormver2.Models.User>

视图需要一组用户,但是会传递给WebForm1。因此错误 - 你必须决定哪一个是正确的,控制器传入WebForm1,或视图,并改变另一个匹配。