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>
}
_Patient.cshtml
. But it doesn't explain what you are having trouble with. Can you please explain the problem you are having, in detail. – Amila