Tôi chỉ mới bắt đầu làm MVC 4 và tôi đã được thưởng thức nó khá một chút, nhưng tôi đang chạy vào rắc rối khi tôi cố gắng lấy cơ sở dữ liệu của tôi (chỉ các mục NAME và EMAIL) để hiển thị tất cả các mục nhập của nó trong khung nhìn chỉ mục. Tôi nhận được lỗi sau:MVC - từ điển đòi hỏi một mục mô hình của loại '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]'.
Tôi có thể kết nối chương trình với cơ sở dữ liệu nhưng tôi bị kẹt ở đây nhiều. Tôi đã tự hỏi nếu tôi có thể nhận được một số trợ giúp. Dưới đây là mã của tôi:
tài Lớp
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.class là một mục cơ sở dữ liệu, có chứa một NAME và lĩnh vực 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());
}
}
}
Index
@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>
}
.210
EDIT: Điều này nói đến tên DataClasses1DataContext. Tôi không chắc điều này sẽ hữu ích như thế nào.
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'
Tôi nhìn bài viết tương tự ở đây và tôi biết nó đã làm với đi qua một đối tượng, nhưng xem là mong đợi cái gì khác. Tuy nhiên tôi sẽ nói rằng tôi là một lập trình viên cấp rất mới bắt đầu và tôi không nghĩ rằng tôi hiểu hầu hết những gì họ đang nói. Tôi về cơ bản theo mã từ trang web MVC hướng dẫn (Link) từ Microsoft về cách hiển thị cơ sở dữ liệu của bạn trong chỉ mục, nhưng tôi nhận được lỗi. Bất kỳ sự hướng dẫn sẽ được đánh giá rất nhiều.
Bạn có thể hiển thị việc triển khai DbContext của mình không? Bạn sẽ cần một DbSet để truy vấn. –
Bạn có thể cung cấp cho tôi hướng dẫn về cách hiển thị implentation không? Lời xin lỗi của tôi, tôi là một người mới bắt đầu thực sự haha. Cảm ơn! –
Ok Tôi đã đi đến các mô hình của mình và có một DataClasses1.dbml. Tôi nhấp chuột phải vào nó và nhấp vào xem mã và bây giờ tôi đang ở trong DataClasses1.cs. trong mã tất cả những gì là: MobileWebFormver2.Models namespace { partial class tên DataClasses1DataContext { }} @LeonCullens –