繁体   English   中英

如何使用带有剃刀视图的实体框架(.edmx模型)为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模型没有定义键。 定义此实体类型的键。

传递到字典中的模型项的类型为“模型”,但是此字典需要类型为“ System.Collections.Generic.IEnumerable”的模型项。

使用通用类型'system.collections.generic.ienumerable需要1个类型参数MVC

目前,我遇到了这个错误,但我感觉到它位于一长串错误中。

传递到字典中的模型项的类型为'FaceToFaceWebsite.Models.PatientListViewModel',但是此字典需要模型项为'System.Collections.Generic.IEnumerable`1 [FaceToFaceWebsite.Models.PatientListViewModel]'。

下面是.edmx (因为我还不能发布图片。

用户 (edmx)-属性-用户ID代号UseBriefInstructions Device_DeviceID-导航属性-设备RegimeItems

设备-属性- 设备 ID名称-导航属性-用户

会话-属性- 会话 ID ActiveUserID ActiveDeviceID StartedAt ReachedFinish-导航属性-SessionExcercises

(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)

视图/首页/_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(我尝试创建另一个db,它将使用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; }
        }    
    }

我已经发布了我认为相关的所有内容,如果您需要更多信息,请告诉我。 如果解决方案很简单,这对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);
        }
    }
}

型号/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; }

    }

}

我目前收到此错误:

System.Web.Mvc.dll中发生类型为'System.InvalidOperationException'的异常,但未在用户代码中处理

附加信息:传递到字典中的模型项的类型为“ System.Collections.Generic.List`1 [FaceToFaceWebsite.Models.Patient]”,但是此字典需要以下类型的模型项

我在“ .. @ Html.Partial(“ _ patient”,Model.PatientProfile)“处收到此错误

_患者(局部视图)

@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());

更改它以返回View(model); 在您的索引函数中。 这将返回您的主页"Views/Home/Index.cshtml" ,并且在此索引视图中,您具有:

@Html.Partial("_Patient", Model)

它将渲染局部视图。 但是,您将错误的模型传递给了索引视图:在Controller中,您传递了PatientListViewModel但是在索引中,您具有: IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>

因此,您要么传递IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>

像这样:

public ActionResult Index(){
return PartialView(new FaceToFaceWebsite.Models.PatientListViewModel());
}

然后您可以像这样调用Partial View:

@model IEnumerable<FaceToFaceWebsite.Models.PatientHomeViewModel>

    @{
        ViewBag.Title = "Home Page";
    }


    @Html.Partial("_Patient", Model)
  1. 你应该怎么做。

    控制器:

     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

}

您的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;
}

更新

    @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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM