I'm having a lot of trouble trying to create a partial view on my home controller from a .edmx
model.
I cannot change the properties of this model because it is a part of another system that I am building this Website around.
I have spent a very long time trying to work this out and have came here to hopefully get some help and advice.
Aim: Render a partial _Patient.cshtml
view on the Homepage using models in an .edmx
model. I would like for you to show me where I have gone wrong as it does not work.
What is the problem?
I get multiple errors such as:
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
Currently I'm getting this error but I get the feeling that it's at the front of a long list of them.
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]'.
Below is the .edmx (as I cannot post images yet..
User (edmx) -Properties- UserID CodeName UseBriefInstructions Device_DeviceID -Navigation Properties- Device RegimeItems
Device -Properties- DeviceID Name -Navigation Properties- Users
Session -Properties- SessionID ActiveUserID ActiveDeviceID StartedAt ReachedFinish -Navigation Properties- SessionExcercises
(the Edmx is alot bigger than shown however these are the models that i currently need to use. however session and user are infact linked through another model)
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 (i tried making another model to as a different approach but to no success)
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(I Tried creating another Db that would use the models from the 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; }
}
}
I've posted everything i see as relevant, if you need anymore information please let me know. I apologise if the solution is simple, im new to MVC and this problem has gone on for quite some time.
---------UPDATE 2.0------------- Views/home/index.cshtml
@model FaceToFaceWebsite.Models.PatientListViewModel
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model.PatientProfile)
views/shared/_Patient.cshtml (partial view)
@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; }
}
}
I am currently getting this error:
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
I am getting this error at "[email protected]("_patient", Model.PatientProfile)"
_Patient (Partial view)
@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.
You are passing back the PartialView as a Result:
return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());
Change this to return View(model);
In your Index function. This will return your Home Page "Views/Home/Index.cshtml"
, and in this Index view you have:
@Html.Partial("_Patient", Model)
Which will Render the Partial View. But you are passing the wrong Model to your Index View: in Controller you are passing PatientListViewModel
But in you index you have: IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
.
So either you pass IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
,
like this:
public ActionResult Index(){
return PartialView(new FaceToFaceWebsite.Models.PatientListViewModel());
}
And then you can call Partial View like this:
@model IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>
@{
ViewBag.Title = "Home Page";
}
@Html.Partial("_Patient", Model)
What you should do.
Controller:
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)
Your _Patient.cshtml (partial View)
@model List<PatientsList>
@foreach(var item in Model){
item.Name
}
Your _Customer.cshtml (partial View) // Another Example
@model List<Custom>
@foreach(var item in Model){
item.Name
}
Your ViewModel:
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;
}
UPDATE
@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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments