MVC表值未显示

时间:2015-11-28 21:32:08

标签: asp.net-mvc model viewmodel

控制器

    public ActionResult Index(int? StaffID, int? id)
    {
        //var userEmail = User.Identity.Name;
        //var model = db.Staffs.Where(i => i.Email == userEmail).Include(x => x.Histories).Include(x => x.CurrentApplications).FirstOrDefault();
        //return View(model);

        var model = new LeaveIndexData();
        var userEmail = User.Identity.Name;

        model.Staffs = db.Staffs
            .Where(i => i.Email == userEmail);

        if(id != null)
        {
            ViewBag.StaffId = id.Value;
            model.CurrentApplications = model.Staffs
                .Where(i => i.StaffID == id.Value).Single().CurrentApplication;
        }

        return View(model);
    }

索引视图

@model test.ViewModel.LeaveIndexData

@{
ViewBag.Title = "Index";
}


@*<table class="table">
<tr>
    <th>Last Name</th>
    <th>First Name</th>
    <th>Hire Date</th>
    <th>Office</th>
    <th>Courses</th>
    <th></th>
</tr>

@foreach (var item in Model.Instructors)
{
    string selectedRow = "";
    if (item.ID == ViewBag.InstructorID)
    {
        selectedRow = "success";
    }
    <tr class="@selectedRow">
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstMidName)*@

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
@foreach (var item in Model.Staffs) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AllocatedLeave)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BalanceLeave)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StaffID }) |
            @Html.ActionLink("Details", "Details", new { id=item.StaffID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.StaffID })
        </td>
    </tr>
}

</table>

@if (Model.CurrentApplications != null)
{ 
<h2>Current Applications</h2>
<table class="table">
    <tr>
        <th>Start Date</th>
        <th>End Date</th>
        <th>Leave Type</th>
        <th>Period</th>
        <th>Application Status</th>
        <th>Leave Reason</th>
    </tr>

    @foreach (var item in Model.CurrentApplications)
    {
        if(item.StaffID == ViewBag.StaffID)
        {
            <tr>
                <td>@item.StartDate</td>
                <td>@item.EndDate</td>
                <td>@item.LeaveType</td>
                <td>@item.NoOfDays</td>
                <td>@item.AppStatus</td>
                <td>@item.LeaveReason</td>
            </tr>
        }
    }
</table>
}

员工班级

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

public partial class Staff
{
    public int StaffID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public Nullable<decimal> AllocatedLeave { get; set; }
    public Nullable<int> BalanceLeave { get; set; }

    public virtual History History { get; set; }
    public virtual ICollection<CurrentApplication> CurrentApplication { get; set; }
}
}

我无法弄清楚为什么我的Current Applications表没有显示。如果我删除了行@if (Model.CurrentApplications != null),我会收到此错误:

  

对象引用未设置为对象的实例

不确定它是否有用,但我正在使用EntityFramework,我的表是从我的数据库生成的?

1 个答案:

答案 0 :(得分:1)

用这个解决了它

<强>控制器

    public ActionResult Index()
    {
        var model = new LeaveIndexData();
        var userEmail = User.Identity.Name;
        model.Staffs = db.Staffs.Single(i => i.Email == userEmail);
        var userID = model.Staffs.StaffID;

        model.CurrentApplications = db.CurrentApplications
            .Where(i => i.StaffID == userID)
            .ToList();

        model.Histories = db.Histories
            .Where(i => i.StaffID == userID).ToList();

        return View(model);
    }

创建了一个名为LeaveIndex Data的视图模型

LeaveIndexData类

public class LeaveIndexData
{
    public Staff Staffs { get; set; }
    public ICollection<CurrentApplication> CurrentApplications { get; set; }
    public IEnumerable<History> Histories { get; set; }

    public IEnumerable<Staff> Staff { get; set; }
    public ICollection<CurrentApplication> DelCurrentApplications { get; set; }
}