MVC表值未显示



控制器

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

我不明白为什么我的"当前应用程序"表没有显示。如果我删除线路@if (Model.CurrentApplications != null),我得到这个错误:

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

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

用这个解决了它

控制器

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

相关内容

  • 没有找到相关文章

最新更新