简体   繁体   English

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

[英]Why is my Partial View is not Showing anything in ASP.NET MVC4 with EDMX model

Im having trouble showing the results from a DataBase in my partial view, while i am getting no errors as a result of a previous question posted the information does not display. 我无法在我的局部视图中显示数据库的结果,但是由于发布的上一个问题而没有出现任何错误,因此该信息不会显示。 I have tried to use viewbags to see if other elements of the page display to no success. 我尝试使用Viewbags查看页面的其他元素是否显示不成功。 However the search bar feature i implemented does show which makes it all the more confusing. 但是我实现的搜索栏功能确实显示出来,这使它更加混乱。

The model properties are taken from an edmx model from another project accessed through the database both projects are connected to. 模型属性来自另一个项目的edmx模型,该项目通过两个项目都连接到的数据库进行访问。

It is my aim to have the partial view show the information of the database on the home index page and to include a search function (which i believe is working but i wont know until i have the partial view displaying. Seeing as i am not getting errors returned my previous question was essentially answered and for anyone going through the same problem would benefit from two solutions to this issue. 我的目的是让局部视图在主页索引页面上显示数据库的信息,并包括搜索功能(我认为这是可行的,但直到显示局部视图时我才知道。)返回的错误基本上可以回答我的上一个问题,对于遇到相同问题的任何人,将从该问题的两种解决方案中受益。

I have narrowed it down to possible options, due to my ability it's up for debate. 由于我的能力,我将其范围缩小到可能的选择,有待辩论。 The edmx model is not being called/referenced by the PatientProfile and PatientListViewModel. PatientProfile和PatientListViewModel不会调用/引用edmx模型。

im not sure if multiple tables F2FDataEntities (the .edmx model) can be used in the same view so i created a listmodel (PatientProfile) and a view model (PatientlistViewModel) 我不确定是否可以在同一视图中使用多个表F2FDataEntities(.edmx模型),所以我创建了一个列表模型(PatientProfile)和一个视图模型(PatientlistViewModel)

HomeController.cs 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 (Model) 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 (Model) 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; }

    }

}

Views/Home/Index.cshtml Views / Home / 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>

Views/Shared/_Patient.cshtml (Partial View) 视图/共享/_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>
    }

Thankyou for taking the time to read this, im still relatively new to MVC and entity so i do apologise if the mistake is stupid. 感谢您抽出宝贵的时间阅读本文,对于MVC和实体,我还是比较陌生的,所以如果错误是愚蠢的,我对此深表歉意。

-Update- -更新-

I have added the line 我加了线

viewModel.PatientProfile = _db.PatientProfiles;

to the home controller, and generated a property stub in 到家庭控制器,并在其中生成属性存根

F2FData.Context.cs (inside of F2FData.edmx, then inside of F2FData.Context.tt) 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; }
    }
}

However the view is still not showing. 但是,该视图仍未显示。

You are not loading anything from the database. 您没有从数据库中加载任何东西。 You create an new instance of the view model, and only assign empty lists, which means the view model is empty, and there's nothing to show in the views. 您创建视图模型的新实例,并仅分配空列表,这意味着视图模型为空,并且视图中没有任何内容可显示。 You wouldn't get an error in that case. 在这种情况下,您不会出错。

It should be something like. 应该是这样的。

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

Notice how we retrieve the patients and profiles from the database? 注意我们如何从数据库中检索患者和档案?

I don't believe you are giving the list any values to loop through. 我不相信您会给列表提供任何要循环的值。

I see here that you create a new viewmodel entity, and a new list, but unless I am missing something the list is never populated 我在这里看到您创建了一个新的viewmodel实体和一个新列表,但是除非我遗漏了某些东西,否则永远不会填充该列表

        var viewModel = new PatientListViewModel();

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

After this line make sure to give the list some actual information to loop through in your partial view, or nothing will display. 在此行之后,请确保为列表提供一些实际信息,以供您在局部视图中浏览,否则将不会显示任何内容。

I typical addition would be something along the lines of: 我通常会添加以下内容:

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

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

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