无法在MVC4中加载嵌套数据

时间:2016-02-10 08:12:52

标签: c# entity-framework

一旦我删除它执行的表之间的关系,就无法在MVC4中加载嵌套数据。当我向我的模型中添加与Employee表有关系的表时,它什么都不加载

我使用2个表Employee和Requests.Added与ADO实体数据模型 这是我的DataController

using Bareed.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Bareed.Controllers
{
    public class DataController : Controller
    {
        //
        // GET: /Data/

        [HttpGet]
        public JsonResult EmployeeRequests()
        {
            List<EmployeeRequests> CO = new List<EmployeeRequests>();
            using ( EmployeeContext dc= new EmployeeContext())
            {
                //var cust = dc.Customers.OrderBy(a => a.ContactName).ToList();
                var cust = dc.Employees.OrderBy(a => a.EmpName).ToList();
                foreach (var i in cust)
                {
                    var requests = dc.Requests.Where(a => a.EmployeeID.Equals(i.EmployeeID)).OrderBy(a => a.RequestDate).ToList();
                    CO.Add(new EmployeeRequests
                    {
                        Employee = i,
                        Requests = requests
                    });
                }
            }
            return new JsonResult { Data = CO, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

    }
}

这是我的DataView.cshtml

    <h2>DataView - Nested Tabuler Data using AngularJS</h2>
<div ng-controller="DataViewController">
    <table class="tableData" border="0" cellspacing="0" cellpadding="0">
        <thead>
           <tr>
               <th></th>
               <th>Employee ID</th>
               <th>Employee Name</th>
               <th>Iqama No</th>
               <th>Company</th>
               <th>Department</th>
               <th>Nationality</th>               
           </tr>
       </thead>
        <tbody ng-repeat="O in Requests">
            <tr ng-class-even="'even'" ng-class-odd="'odd'">

                <td class="CX"><span>+</span></td>
                <td>{{O.Employee.EmployeeID}}</td>
                <td>{{O.Employee.EmpName}}</td>
                <td>{{O.Employee.IqamaNo}}</td>
                <td>{{O.Employee.CompanyID}}</td>
                <td>{{O.Employee.DepartmentID}}</td>
                <td>{{O.Employee.NationalityID}}</td>
            </tr>
            <tr class="sub">
                <td></td>
                <td colspan="8">
                    <table class="tableData" border="0" cellspacing="0" cellpadding="0" >
                        <tr>
                            <th>Request ID</th>
                            <th>Employee ID</th>
                            <th>Iqama Number</th>
                            <th>Request Type</th>
                            <th>Request Date</th>
                            <th>Email Address</th>
                            <th>Agent Name</th>
                            <th>Agent Contact</th>
                        </tr>
                        <tr ng-repeat="a in O.Requests" ng-class-even="'even'" ng-class-odd="'odd'">

                            <td>{{a.RequestID}}</td>
                            <td>{{a.EmployeeID}}</td>
                            <td>{{a.IqamaNo}}</td>
                            <td>{{a.RequestType}}</td>
                            <td>{{a.RequestDate.slice(6,-2) | date:'dd-MM-yyyy'}}</td>                           
                            <td>{{a.EmailAddress}}</td>
                            <td>{{a.AgentName}}</td>
                            <td>{{a.AgentContact}}</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>

这是我的Employee.cs类,它是由Data Model自动生成的。而且我想显示公司名称,部门名称,国籍而不是他们的ID。在我的DataView中。

    namespace Bareed.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Employee
    {
        public int EmployeeID { get; set; }
        public string EmpName { get; set; }
        public string IqamaNo { get; set; }
        public Nullable<int> CompanyID { get; set; }
        public Nullable<int> DepartmentID { get; set; }
        public Nullable<int> NationalityID { get; set; }

        public virtual Company Company { get; set; }
        public virtual Department Department { get; set; }
        public virtual Nationality Nationality { get; set; }
    }
}

0 个答案:

没有答案