Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的局部视图在ASP.NET MVC4和EDMX模型中没有显示任何内容_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Edmx - Fatal编程技术网

C# 为什么我的局部视图在ASP.NET MVC4和EDMX模型中没有显示任何内容

C# 为什么我的局部视图在ASP.NET MVC4和EDMX模型中没有显示任何内容,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,edmx,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Edmx,在我的局部视图中显示数据库的结果时,我遇到了问题,而由于先前发布的问题,我没有收到任何错误。信息没有显示。我尝试使用viewbags查看页面的其他元素是否显示不成功。然而,我实现的搜索栏功能确实显示了这一点,这使得它更加混乱 模型属性取自通过两个项目所连接的数据库访问的另一个项目的edmx模型 我的目标是让局部视图在主索引页上显示数据库的信息,并包含搜索功能(我相信这是可行的,但在显示局部视图之前我不会知道。鉴于我没有收到返回的错误,我的上一个问题基本上得到了回答,对于任何经历相同问题的人来说,

在我的局部视图中显示数据库的结果时,我遇到了问题,而由于先前发布的问题,我没有收到任何错误。信息没有显示。我尝试使用viewbags查看页面的其他元素是否显示不成功。然而,我实现的搜索栏功能确实显示了这一点,这使得它更加混乱

模型属性取自通过两个项目所连接的数据库访问的另一个项目的edmx模型

我的目标是让局部视图在主索引页上显示数据库的信息,并包含搜索功能(我相信这是可行的,但在显示局部视图之前我不会知道。鉴于我没有收到返回的错误,我的上一个问题基本上得到了回答,对于任何经历相同问题的人来说,将受益于此问题的两种解决方案

由于我的能力,我已经将其缩小到了可能的选项范围。edmx模型没有被PatientProfile和PatientListViewModel调用/引用

我不确定是否可以在同一个视图中使用多个表f2fdataenties(.edmx模型),因此我创建了一个列表模型(PatientProfile)和一个视图模型(PatientlistViewModel)

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 = new F2FDataEntities();

        public ActionResult Index(string searchTerm = null, int page = 1)
        {
            var viewModel = new PatientListViewModel();

            viewModel.PatientProfile = new List<PatientProfile>();

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Patient", viewModel);
            }

            return View(viewModel);

        }

        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);
        }
    }

}
到主控制器,并在中生成属性存根

F2FData.Context.cs(在F2FData.edmx内部,然后在F2FData.Context.tt内部)

//------------------------------------------------------------------------------
// 
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
// 
//------------------------------------------------------------------------------
命名空间FaceToFaceWebsite.Models
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类F2FDataEntities:DbContext
{
公共F2FDataEntities()
:base(“name=F2FDataEntities”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共数据库集C__迁移历史{get;set;}
公共数据库集设备{get;set;}
公共数据库集练习{get;set;}
公共DbSet PoseChannels{get;set;}
公共DbSet Poses{get;set;}
公共DbSet RegimeItems{get;set;}
公共数据库集ScreenInteractionEntries{get;set;}
公共DbSet SessionExercises{get;set;}
公共数据库集会话{get;set;}
公共数据库集用户{get;set;}
public System.Collections.Generic.List PatientProfiles{get;set;}
}
}

但是视图仍然没有显示。

您没有从数据库加载任何内容。您创建了视图模型的新实例,并且只分配了空列表,这意味着视图模型是空的,并且视图中没有显示任何内容。在这种情况下,不会出现错误

应该是这样的

var viewModel = new PatientListViewModel();
viewModel.PatientProfile = _db.PatientProfiles;
return View(viewModel); 

注意我们是如何从数据库中检索患者和个人资料的?

我认为您没有给列表提供任何可循环使用的值

我在这里看到,您创建了一个新的viewmodel实体和一个新列表,但除非我遗漏了什么,否则该列表永远不会填充

        var viewModel = new PatientListViewModel();

        viewModel.PatientProfile = new List<PatientProfile>();

谢谢,我已经在我的问题中添加了一个更新,让您了解正在发生的事情。我最初遗漏了这一点,因为我不知道它的存在。F2FData.Context.CS包含我正在使用的edmx模型的数据库集。@Nilmag PatientProfiles不应该是DbContext中的
列表
。它应该是
数据库集
。和
PatientProofile
不应从
DbSet
继承。您应该只有一个DbContext,数据库中的每一组数据都有一个
DbSet
。我现在在:viewModel.PatientProfile=\u db.PatientProfile;处收到一个错误,即“无法隐式地将type”System.data.Entity.DbSet“转换为”System.Collections.Generic.List”@Nilmag将您的视图模型更改为接受
IEnumerable
而不是
List
。您的视图也是如此。感谢您修复了错误,但是除了搜索功能之外,我仍然没有从我的局部视图中获得任何信息。抱歉。谢谢,我已经按照您和mason所述的方式完成了操作,但是相同的问题仍然存在。我添加了一个n更新我的问题,让您了解情况。无论您有什么代码,如果您没有向列表提供任何数据,那么您的foreach()循环将没有任何可循环的内容,因此没有数据。在将列表发送到局部视图之前,您必须先填充列表。
@model FaceToFaceWebsite.Models.PatientListViewModel

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

@using(Ajax.BeginForm(
    new AjaxOptions{
    HttpMethod="get",
    InsertionMode=InsertionMode.Replace,
    UpdateTargetId="patientList"}))

{
    <input type="search" name="searchTerm" />
    <input type="submit" value="Search By Name" />
}
@Html.Partial("~/Views/Shared/_Patient.cshtml", Model.PatientProfile)

<form method="get" action="@Url.Action("Index")" data-f2fw-ajax="true" data-f2fw-target="#patientList">
    <input type="text" name="searchTerm" data-f2fw-autocomplete="@Url.Action("Autocomplete")" />
    <input type="submit" value="Search By Name" />
</form>
@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>
    }
viewModel.PatientProfile = _db.PatientProfiles;
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace FaceToFaceWebsite.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class F2FDataEntities : DbContext
    {
        public F2FDataEntities()
            : base("name=F2FDataEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
        public DbSet<Device> Devices { get; set; }
        public DbSet<Exercis> Exercises { get; set; }
        public DbSet<PoseChannel> PoseChannels { get; set; }
        public DbSet<Pos> Poses { get; set; }
        public DbSet<RegimeItem> RegimeItems { get; set; }
        public DbSet<ScreenInteractionsEntry> ScreenInteractionsEntries { get; set; }
        public DbSet<SessionExercis> SessionExercises { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<User> Users { get; set; }

        public System.Collections.Generic.List<PatientProfile> PatientProfiles { get; set; }
    }
}
var viewModel = new PatientListViewModel();
viewModel.PatientProfile = _db.PatientProfiles;
return View(viewModel); 
        var viewModel = new PatientListViewModel();

        viewModel.PatientProfile = new List<PatientProfile>();
viewmodel.PatientProfile = _db.Example......(get a list, or values from database)