How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

Nilmag

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>
}
Dawood Awan

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 PatientListViewModelBut 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)
  1. 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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I pass parameters to a partial view in mvc 4

From Dev

MVC 4 Razor, Posting form with partial views

From Dev

How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

From Dev

How can jQuery DataTables be applied to AJAX rendered partial view in MVC4?

From Dev

Can MVC 5 scaffolding not add selectlist for many-many relations? (Add Scaffold - MVC 5 Controller with views, using Entity Framework)

From Dev

Rendering partial views with Razor in MVC5

From Dev

How can I pass a String as a model to a View in MVC 5?

From Dev

Render partial views inside main view in mvc4

From Dev

How to bind dropdownlist in partial view in mvc 4 using model?

From Dev

MVC razor partial view radio button selection for each row in model

From Dev

mvc 4 - c# - Entity Framework - Metadata using edmx file

From Dev

Asp.net mvc4 Partial view with model in Layout

From Dev

How to update view model in a partial view in MVC5?

From Dev

MVC Edit and Send Model View with Partial Views

From Dev

partial update on an mvc model in razor using js

From Dev

How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

From Dev

How can jQuery DataTables be applied to AJAX rendered partial view in MVC4?

From Dev

Can MVC 5 scaffolding not add selectlist for many-many relations? (Add Scaffold - MVC 5 Controller with views, using Entity Framework)

From Dev

Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

From Dev

Return Partial view with model c# mvc4

From Dev

How to pass multiples and selective Model into MVC razor Partial View?

From Dev

mvc4 Razor Ajax Call to show partial view

From Dev

How can I pass a String as a model to a View in MVC 5?

From Dev

How can I get host in a Controller using MVC4

From Dev

Hacking Razor partial views into a WebForms project in MVC5

From Dev

MVC 5 Partial View - Model confusion

From Dev

required attribute not generating in mvc partial razor view (using Edmx Models)

From Dev

how to create strongly typed partial view from view model in mvc?

From Dev

@using by default in razor views MVC 5

Related Related

  1. 1

    How can I pass parameters to a partial view in mvc 4

  2. 2

    MVC 4 Razor, Posting form with partial views

  3. 3

    How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

  4. 4

    How can jQuery DataTables be applied to AJAX rendered partial view in MVC4?

  5. 5

    Can MVC 5 scaffolding not add selectlist for many-many relations? (Add Scaffold - MVC 5 Controller with views, using Entity Framework)

  6. 6

    Rendering partial views with Razor in MVC5

  7. 7

    How can I pass a String as a model to a View in MVC 5?

  8. 8

    Render partial views inside main view in mvc4

  9. 9

    How to bind dropdownlist in partial view in mvc 4 using model?

  10. 10

    MVC razor partial view radio button selection for each row in model

  11. 11

    mvc 4 - c# - Entity Framework - Metadata using edmx file

  12. 12

    Asp.net mvc4 Partial view with model in Layout

  13. 13

    How to update view model in a partial view in MVC5?

  14. 14

    MVC Edit and Send Model View with Partial Views

  15. 15

    partial update on an mvc model in razor using js

  16. 16

    How can I map tables using fluent API in ASP.NET MVC 5, Entity Framework 6?

  17. 17

    How can jQuery DataTables be applied to AJAX rendered partial view in MVC4?

  18. 18

    Can MVC 5 scaffolding not add selectlist for many-many relations? (Add Scaffold - MVC 5 Controller with views, using Entity Framework)

  19. 19

    Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

  20. 20

    Return Partial view with model c# mvc4

  21. 21

    How to pass multiples and selective Model into MVC razor Partial View?

  22. 22

    mvc4 Razor Ajax Call to show partial view

  23. 23

    How can I pass a String as a model to a View in MVC 5?

  24. 24

    How can I get host in a Controller using MVC4

  25. 25

    Hacking Razor partial views into a WebForms project in MVC5

  26. 26

    MVC 5 Partial View - Model confusion

  27. 27

    required attribute not generating in mvc partial razor view (using Edmx Models)

  28. 28

    how to create strongly typed partial view from view model in mvc?

  29. 29

    @using by default in razor views MVC 5

HotTag

Archive