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

时间:2015-02-09 14:30:50

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

我在部分视图中显示来自DataBase的结果时遇到问题,而由于之前发布的问题我没有收到任何错误,因此信息无法显示。我试图使用viewbags来查看页面的其他元素是否显示不成功。然而,我实现的搜索栏功能确实显示哪个更令人困惑。

模型属性来自另一个通过数据库访问的项目的edmx模型,两个项目都连接到。

我的目标是让部分视图在主索引页面上显示数据库的信息并包含搜索功能(我相信它正在工作但我不会知道,直到我显示部分视图。看到我我没有收到错误我之前的问题基本上得到了回答,任何遇到同样问题的人都可以从这个问题的两个解决方案中受益。

我已将其缩小到可能的选项,因为我有能力进行辩论。 PatientProfile和PatientListViewModel不会调用/引用edmx模型。

我不确定是否可以在同一视图中使用多个表F2FDataEntities(.edmx模型),因此我创建了一个listmodel(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);
        }
    }

}

PatientListViewModel.cs (型号)

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 List<PatientProfile> PatientProfile { get; set; }
    }

    public class Patient
    {
        public IEnumerable<User> UserID { get; set; }
        public IEnumerable<User> CodeName { get; set; }
        public IEnumerable<Device> Name { get; set; }
        public IEnumerable<Session> ReachedFinish { get; set; }
    }
}

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 IEnumerable<User> UserID { get; set; }
       public IEnumerable<User> CodeName { get; set; }
       public IEnumerable<Device> Name { get; set; }
       public IEnumerable<Session> ReachedFinish { get; set; }

    }

}

查看/主页/ Index.cshtml

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

观看/共享/ _Patient.cshtml (部分视图)

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

谢谢您花时间阅读本文,我对MVC和实体仍然相对较新,所以如果错误是愚蠢的话,我会道歉。

-Update -

我添加了一行

viewModel.PatientProfile = _db.PatientProfiles;

到家庭控制器,并在

中生成属性存根

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

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

但视图仍未显示。

2 个答案:

答案 0 :(得分:1)

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

它应该是类似的东西。

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

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

答案 1 :(得分:1)

我不相信你给列表提供了循环的任何值。

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

        var viewModel = new PatientListViewModel();

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

在此行之后,请务必在列表视图中为列表提供一些实际信息,否则将显示任何内容。

我通常会添加以下内容:

viewmodel.PatientProfile = _db.Example......(get a list, or values from database)