我如何使用 Entity Framework(.edmx 模型)和 Razor 视图为 MVC4 或 MVC 5 创建局部视图?
How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?
我在尝试通过 .edmx
模型在我的家庭控制器上创建局部视图时遇到了很多麻烦。
我无法更改此模型的属性,因为它是我正在构建此网站的另一个系统的一部分。
我花了很长时间试图解决这个问题,来到这里希望能得到一些帮助和建议。
目标: 使用 .edmx
模型中的模型在主页上呈现部分 _Patient.cshtml
视图。我希望你能告诉我哪里出了问题,因为它不起作用。
有什么问题?
我收到多个错误,例如:
model: : EntityType model has no key defined. Define the key for this entity type.
The model item passed into the dictionary is of type 'Models', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`
using the generic type 'system.collections.generic.ienumerable requires 1 type arguments MVC
目前我得到这个错误,但我感觉它在一长串错误列表的前面。
The model item passed into the dictionary is of type 'FaceToFaceWebsite.Models.PatientListViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[FaceToFaceWebsite.Models.PatientListViewModel]'.
下面是.edmx
(因为我还不能 post 图片..
用户 (edmx)
-特性-
用户身份
代码名称
使用简要说明
Device_DeviceID
-导航属性-
设备
制度项目
设备
-特性-
设备ID
姓名
-导航属性-
用户
会话
-特性-
会话ID
活动用户ID
ActiveDeviceID
开始于
到达终点
-导航属性-
会话练习
(Edmx 比显示的大很多,但是这些是我目前需要使用的模型。但是会话和用户实际上是通过另一个模型链接的)
HomeController.cs
using FaceToFaceWebsite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using PagedList;
using System.Web.UI;
using System.Configuration;
using System.Collections;
namespace FaceToFaceWebsite.Controllers
{
public class HomeController : Controller
{
public F2FDataEntities _db = F2FDataEntities();
[OutputCache(CacheProfile = "Long", VaryByHeader = "X-Requested-With", Location = OutputCacheLocation.Server)]
public ActionResult Index(string searchTerm = null, int page = 1)
//public ActionResult Index()
{
var model =
_db.UserID.ToList()
.OrderByDescending(u =>u.UserID)
.Take(10)
.Select (u => new PatientListViewModel
{
CodeName = u.CodeName,
Name = u.Name
}).ToPagedList(page, 10);
return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());
////return View(model);
////ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Patients()
{
ViewBag.Message = "";
return View();
}
public ActionResult Help()
{
ViewBag.Message = "";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Contact Us";
return View();
}
protected override void Dispose(bool disposing)
{
if (_db != null)
{
_db.Dispose();
}
base.Dispose(disposing);
}
}
}
PatientListViewModel
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Entity;
namespace FaceToFaceWebsite.Models
{
public class PatientListViewModel
{
public virtual ICollection<User> CodeName { get; set; }
public virtual ICollection<Session> ReachedFinish { get; set; }
public virtual ICollection<Device> Name { get; set; }
}
}
Views/Home/Index.cshtml
System.Collections.Generic.IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>*@
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model)
Views/Home/_Patient.cshtml
@model IEnumerable<PatientListViewModel>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
PatientProfile.cs(我尝试制作另一个模型作为不同的方法但没有成功)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FaceToFaceWebsite.Models
{
public class PatientProfile : DbContext
{
public PatientProfile() : base("F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<Device> Name { get; set; }
//public DbSet<RestaurantReview> Reviews { get; set; }
public System.Data.Entity.DbSet<FaceToFaceWebsite.Models.PatientListViewModel> PatientListViewModels { get; set; }
}
}
F2FDataDB.cs(我尝试创建另一个使用 edmx 模型的数据库)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FaceToFaceWebsite.Models
{
public class F2FDataEntities : DbContext
{
public F2FDataEntities() : base("name=F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<User>CodeName { get; set; }
//public DbSet<Session> SessionDB { get; set; }
public DbSet<Device> Name { get; set; }
public System.Data.Entity.DbSet<FaceToFaceWebsite.Models.PatientListViewModel> PatientListViewModel { get; set; }
}
}
我已经 post 编辑了我认为相关的所有内容,如果您需要更多信息,请告诉我。
如果解决方案很简单,我深表歉意,我是 MVC 的新手,这个问题已经持续了很长时间。
--------更新 2.0------------
Views/home/index.cshtml
@model FaceToFaceWebsite.Models.PatientListViewModel
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model.PatientProfile)
views/shared/_Patient.cshtml(部分视图)
@model List<PatientProfile>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<div>
@*<span>UserName: @item.CodeName</span>*@
</div>
@*<span>Started Session at: @item.StartedAt</span>*@
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
patientlistviewmodel.cs
namespace FaceToFaceWebsite.Models
{
public class PatientListViewModel
{
public List<Patient> PatientProfile { get; set; }
}
public class Patient
{
public int UserID { get; set; }
public string CodeName { get; set; }
}
}
HomeController.cs
namespace FaceToFaceWebsite.Controllers
{
public class HomeController : Controller
{
public F2FDataEntities _db = new F2FDataEntities();
public ActionResult Index()
{
var viewModel = new PatientListViewModel();
viewModel.PatientProfile = new List<Patient>();
return View(viewModel);
}
}
}
Models/PatientProfile.cs
namespace FaceToFaceWebsite.Models
{
public class PatientProfile : DbContext
{
public PatientProfile() : base("F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<User> CodeName{ get; set; }
public DbSet<Device> Name { get; set; }
public DbSet<Session> ReachedFinish { get; set; }
}
}
我目前收到此错误:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FaceToFaceWebsite.Models.Patient]', but this dictionary requires a model item of type
我在 "..@Html.Partial("_patient", Model.PatientProfile)"
处收到此错误
_Patient(局部视图)
@model List<PatientProfile>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
1.
您正在将 PartialView 作为结果传回:
return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());
在您的 Index 函数中将其更改为 return View(model);
。这将 return 您的主页 "Views/Home/Index.cshtml"
,并且在此索引视图中您有:
@Html.Partial("_Patient", Model)
这将渲染局部视图。但是您将错误的模型传递给了您的索引视图:在控制器中您传递了 PatientListViewModel
但是在您的索引中您有:IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
.
所以要么你通过 IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
,
像这样:
public ActionResult Index(){
return PartialView(new FaceToFaceWebsite.Models.PatientListViewModel());
}
然后你可以像这样调用局部视图:
@model IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model)
你应该做什么
控制器:
public ActionResult Index()
{
var viewModel = PatientListViewModel();
viewModel.PatientList = new List<Patients>();
viewModel.CustomerList = new List<Cusotmer>();
return View(viewModel);
}
Index.cshtml
@model PatientListViewModel
@* Considering your partial view is in Shared Foler*@
// Note Passing the Correct Model to Partial View
@Html.Partial("_Patient", Model.PatientsList)
// Another Example:
@Html.Partial("_Customer", Model.CustomerList)
您的_Patient.cshtml(局部视图)
@model List<PatientsList>
@foreach(var item in Model){
item.Name
}
你的_Customer.cshtml(局部视图) // 另一个例子
@model List<Custom>
@foreach(var item in Model){
item.Name
}
你的视图模型:
public class PatientListViewModel{
public List<Patient> PatientsList {get;set;}
public List<Customer> CustomerList{get;set;}
}
public class Patient{
public string Name {get;set;
}
// Another Example
public Class Customer{
public string Name{get;set;
}
更新
@model FaceToFaceWebsite.Models.PatientListViewModel
@{
ViewBag.Title = "Home Page";
}
// PatientListViewModel this is the Name of you class
// To Access your Model you always have to use Model. Not the Class Name
// PatientListViewModel is the Class name
// Model is the Object --> You passed from Controller.
// Use Model.PatientProfile
// Not PatientListViewModel.PatientProfile
@Html.Partial("_Patient", Model.PatientProfile)
我在尝试通过 .edmx
模型在我的家庭控制器上创建局部视图时遇到了很多麻烦。
我无法更改此模型的属性,因为它是我正在构建此网站的另一个系统的一部分。
我花了很长时间试图解决这个问题,来到这里希望能得到一些帮助和建议。
目标: 使用 .edmx
模型中的模型在主页上呈现部分 _Patient.cshtml
视图。我希望你能告诉我哪里出了问题,因为它不起作用。
有什么问题?
我收到多个错误,例如:
model: : EntityType model has no key defined. Define the key for this entity type.
The model item passed into the dictionary is of type 'Models', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`
using the generic type 'system.collections.generic.ienumerable requires 1 type arguments MVC
目前我得到这个错误,但我感觉它在一长串错误列表的前面。
The model item passed into the dictionary is of type 'FaceToFaceWebsite.Models.PatientListViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[FaceToFaceWebsite.Models.PatientListViewModel]'.
下面是.edmx (因为我还不能 post 图片..
用户 (edmx) -特性- 用户身份 代码名称 使用简要说明 Device_DeviceID -导航属性- 设备 制度项目
设备 -特性- 设备ID 姓名 -导航属性- 用户
会话 -特性- 会话ID 活动用户ID ActiveDeviceID 开始于 到达终点 -导航属性- 会话练习
(Edmx 比显示的大很多,但是这些是我目前需要使用的模型。但是会话和用户实际上是通过另一个模型链接的)
HomeController.cs
using FaceToFaceWebsite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using PagedList;
using System.Web.UI;
using System.Configuration;
using System.Collections;
namespace FaceToFaceWebsite.Controllers
{
public class HomeController : Controller
{
public F2FDataEntities _db = F2FDataEntities();
[OutputCache(CacheProfile = "Long", VaryByHeader = "X-Requested-With", Location = OutputCacheLocation.Server)]
public ActionResult Index(string searchTerm = null, int page = 1)
//public ActionResult Index()
{
var model =
_db.UserID.ToList()
.OrderByDescending(u =>u.UserID)
.Take(10)
.Select (u => new PatientListViewModel
{
CodeName = u.CodeName,
Name = u.Name
}).ToPagedList(page, 10);
return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());
////return View(model);
////ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Patients()
{
ViewBag.Message = "";
return View();
}
public ActionResult Help()
{
ViewBag.Message = "";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Contact Us";
return View();
}
protected override void Dispose(bool disposing)
{
if (_db != null)
{
_db.Dispose();
}
base.Dispose(disposing);
}
}
}
PatientListViewModel
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Entity;
namespace FaceToFaceWebsite.Models
{
public class PatientListViewModel
{
public virtual ICollection<User> CodeName { get; set; }
public virtual ICollection<Session> ReachedFinish { get; set; }
public virtual ICollection<Device> Name { get; set; }
}
}
Views/Home/Index.cshtml
System.Collections.Generic.IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>*@
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model)
Views/Home/_Patient.cshtml
@model IEnumerable<PatientListViewModel>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
PatientProfile.cs(我尝试制作另一个模型作为不同的方法但没有成功)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FaceToFaceWebsite.Models
{
public class PatientProfile : DbContext
{
public PatientProfile() : base("F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<Device> Name { get; set; }
//public DbSet<RestaurantReview> Reviews { get; set; }
public System.Data.Entity.DbSet<FaceToFaceWebsite.Models.PatientListViewModel> PatientListViewModels { get; set; }
}
}
F2FDataDB.cs(我尝试创建另一个使用 edmx 模型的数据库)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace FaceToFaceWebsite.Models
{
public class F2FDataEntities : DbContext
{
public F2FDataEntities() : base("name=F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<User>CodeName { get; set; }
//public DbSet<Session> SessionDB { get; set; }
public DbSet<Device> Name { get; set; }
public System.Data.Entity.DbSet<FaceToFaceWebsite.Models.PatientListViewModel> PatientListViewModel { get; set; }
}
}
我已经 post 编辑了我认为相关的所有内容,如果您需要更多信息,请告诉我。 如果解决方案很简单,我深表歉意,我是 MVC 的新手,这个问题已经持续了很长时间。
--------更新 2.0------------ Views/home/index.cshtml
@model FaceToFaceWebsite.Models.PatientListViewModel
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model.PatientProfile)
views/shared/_Patient.cshtml(部分视图)
@model List<PatientProfile>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<div>
@*<span>UserName: @item.CodeName</span>*@
</div>
@*<span>Started Session at: @item.StartedAt</span>*@
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
patientlistviewmodel.cs
namespace FaceToFaceWebsite.Models
{
public class PatientListViewModel
{
public List<Patient> PatientProfile { get; set; }
}
public class Patient
{
public int UserID { get; set; }
public string CodeName { get; set; }
}
}
HomeController.cs
namespace FaceToFaceWebsite.Controllers
{
public class HomeController : Controller
{
public F2FDataEntities _db = new F2FDataEntities();
public ActionResult Index()
{
var viewModel = new PatientListViewModel();
viewModel.PatientProfile = new List<Patient>();
return View(viewModel);
}
}
}
Models/PatientProfile.cs
namespace FaceToFaceWebsite.Models
{
public class PatientProfile : DbContext
{
public PatientProfile() : base("F2FDataEntities")
{
}
public DbSet<User> UserID { get; set; }
public DbSet<User> CodeName{ get; set; }
public DbSet<Device> Name { get; set; }
public DbSet<Session> ReachedFinish { get; set; }
}
}
我目前收到此错误:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FaceToFaceWebsite.Models.Patient]', but this dictionary requires a model item of type
我在 "..@Html.Partial("_patient", Model.PatientProfile)"
处收到此错误_Patient(局部视图)
@model List<PatientProfile>
@foreach (var item in Model)
{
<div>
<h4>UserID: @item.CodeName</h4>
<span>Finished: @item.ReachedFinish</span>
<p>Machine: @item.Name</p>
<hr />
</div>
}
1.
您正在将 PartialView 作为结果传回:
return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());
在您的 Index 函数中将其更改为 return View(model);
。这将 return 您的主页 "Views/Home/Index.cshtml"
,并且在此索引视图中您有:
@Html.Partial("_Patient", Model)
这将渲染局部视图。但是您将错误的模型传递给了您的索引视图:在控制器中您传递了 PatientListViewModel
但是在您的索引中您有:IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
.
所以要么你通过 IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
,
像这样:
public ActionResult Index(){
return PartialView(new FaceToFaceWebsite.Models.PatientListViewModel());
}
然后你可以像这样调用局部视图:
@model IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model)
你应该做什么
控制器:
public ActionResult Index() { var viewModel = PatientListViewModel(); viewModel.PatientList = new List<Patients>(); viewModel.CustomerList = new List<Cusotmer>(); return View(viewModel); }
Index.cshtml
@model PatientListViewModel
@* Considering your partial view is in Shared Foler*@
// Note Passing the Correct Model to Partial View
@Html.Partial("_Patient", Model.PatientsList)
// Another Example:
@Html.Partial("_Customer", Model.CustomerList)
您的_Patient.cshtml(局部视图)
@model List<PatientsList>
@foreach(var item in Model){
item.Name
}
你的_Customer.cshtml(局部视图) // 另一个例子
@model List<Custom>
@foreach(var item in Model){
item.Name
}
你的视图模型:
public class PatientListViewModel{
public List<Patient> PatientsList {get;set;}
public List<Customer> CustomerList{get;set;}
}
public class Patient{
public string Name {get;set;
}
// Another Example
public Class Customer{
public string Name{get;set;
}
更新
@model FaceToFaceWebsite.Models.PatientListViewModel
@{
ViewBag.Title = "Home Page";
}
// PatientListViewModel this is the Name of you class
// To Access your Model you always have to use Model. Not the Class Name
// PatientListViewModel is the Class name
// Model is the Object --> You passed from Controller.
// Use Model.PatientProfile
// Not PatientListViewModel.PatientProfile
@Html.Partial("_Patient", Model.PatientProfile)