【问题标题】:How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?如何使用带有 Razor 视图的实体框架(.edmx 模型)为 MVC4 或 MVC 5 创建局部视图?
【发布时间】:2015-02-02 11:33:23
【问题描述】:

我在尝试使用 .edmx 模型在我的家庭控制器上创建局部视图时遇到了很多麻烦。

我无法更改此模型的属性,因为它是我正在构建此网站的另一个系统的一部分。

我花了很长时间试图解决这个问题,并希望能得到一些帮助和建议。

目标:使用.edmx 模型中的模型在主页上呈现部分_Patient.cshtml 视图。我想让你告诉我哪里出了问题,因为它不起作用。

有什么问题?

我收到多个错误,例如:

model: : EntityType 模型没有定义键。定义此实体类型的键。

传入字典的模型项是“Models”类型,但该字典需要一个“System.Collections.Generic.IEnumerable”类型的模型项

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

目前我遇到了这个错误,但我感觉它排在一长串的前面。

传入字典的模型项的类型为“FaceToFaceWebsite.Models.PatientListViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[FaceToFaceWebsite.Models.PatientListViewModel]”的模型项。

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

用户 (edmx) -特性- 用户身份 代码名称 使用说明 Device_DeviceID -导航属性- 设备 政权物品

设备 -特性- 设备编号 姓名 -导航属性- 用户

会话 -特性- 会话ID 活动用户 ID 活动设备ID 开始于 达到完成 -导航属性- 会话练习

(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 模型的 Db)

    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------------- 视图/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)”处收到此错误

_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>
}

【问题讨论】:

  • 抱歉,我改了标题来解决这个问题。
  • 不,请用您的实际问题更新您的问题正文。目前尚不清楚您到底在问我们什么。问题是什么?你期望它做什么,它做什么,为什么不做什么?
  • 据我所知,您已经创建了一个局部视图_Patient.cshtml。但这并不能解释您遇到的问题。你能详细解释一下你遇到的问题吗?
  • 希望这更清楚一点。

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 razor


【解决方案1】:

1.

您将 PartialView 作为结果传回:

return PartialView("_Patient", new FaceToFaceWebsite.Models.PatientListViewModel());

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

@Html.Partial("_Patient", Model)

这将渲染局部视图。但是您将错误的模型传递给您的索引视图:在控制器中您正在传递PatientListViewModel但在您的索引中您有:IEnumerable&lt;FaceToFaceWebsite.Models.PatientHomeViewModel&gt;

所以要么你通过IEnumerable&lt;FaceToFaceWebsite.Models.PatientHomeViewModel&gt;

像这样:

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

然后你可以像这样调用部分视图:

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

【讨论】:

  • 感谢您到目前为止的帮助 :) 使用 HomeController 我会收到“PatientListViewModel”的错误,说它是“一个'类型',但用作'变量'。对于“列表” 我得到“使用泛型类型 @system.Collections.Generic.List' 需要 1 个类型参数”。我也为 PatientHomeViewController 道歉,这是我没有纠正的错误。应该是 PatientListViewModel。跨度>
  • 此代码未在 Visual Studio 中测试,我在 StackOverflow 中输入。 var viewModel = PatientListViewModel();应该是 var viewModel = new PatientListViewModel();
  • 谢谢! “viewModel.PatientProfile = new List();”怎么样?我得到“使用泛型类型 @system.Collections.Generic.List' 需要 1 个类型参数”。
  • viewModel.PatientProfile = 新列表();
  • viewModel.PatientProfile = 新列表();
猜你喜欢
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
相关资源
最近更新 更多