首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在视图中显示模型中的数据的ASP.NET MVC?

在视图中显示模型中的数据的ASP.NET MVC?
EN

Stack Overflow用户
提问于 2019-01-16 23:12:00
回答 1查看 57关注 0票数 1

我已经从另外两个模型EmployeeHolidayRequestForm创建了一个视图模型EmployeeViewModel,因为我需要在一个视图中使用它们。我的视图显示模型中的数据时遇到问题。以下是我的模型:

代码语言:javascript
运行
复制
public partial class Employee
{
    public int EmployeeID { get; set; }
    public string FullName { get; set; }
    public string EmailID { get; set; }
    public string Password { get; set; }
    public System.DateTime StartDate { get; set; }
    public int RoleID { get; set; }
    public int ShiftID { get; set; }
    public int AreaID { get; set; }
    public int DisciplineID { get; set; }
    public int SiteID { get; set; }
    public int ALCategory { get; set; }
    public Nullable<int> HoursTaken { get; set; }
    public Nullable<int> AwardedLeave { get; set; }
    public Nullable<int> TotalHoursThisYear { get; set; }
    public int HoursCarriedForward { get; set; }
    public Nullable<int> EntitlementRemainingThisYear { get; set; }
    public string Comments { get; set;}

}

假期申请表模型:

代码语言:javascript
运行
复制
 public partial class HolidayRequestForm
{
    public int RequestID { get; set; }
    public int EmployeeID { get; set; }
    public System.DateTime StartDate { get; set; }
    public System.DateTime FinishDate { get; set; }
    public int HoursTaken { get; set; }
    public string Comments { get; set; }
    public int YearCreated { get; set; }
    public int MonthCreated { get; set; }
    public Nullable<int> DayCreated { get; set; }
    public Nullable<int> YearOfHoliday { get; set; }

    public virtual Employee Employee { get; set; }
}

员工视图模型:

代码语言:javascript
运行
复制
 public class EmployeeViewModel
{
    public Employee Employee { get; set; }

    public List<HolidayRequestForm> HolidayRequestForm { get; set; }
}

下面是我的控制器操作:

代码语言:javascript
运行
复制
   public ActionResult Details(int? id)
    {
        Employee employee =  db.Employees.FirstOrDefault(emp => 
 emp.EmployeeID == id);
        List<HolidayRequestForm> holidayRequestForm = 
 db.HolidayRequestForms.Where(emp => emp.EmployeeID == id).ToList();

 EmployeeViewModel employeeViewModel = new EmployeeViewModel()
           {
               Employee = employee,
               HolidayRequestForm = holidayRequestForm,
           };

        return View(employeeViewModel);


    }

我正在尝试将这个模型实现到我的视图中,但是我遇到了麻烦。以下是视图中引起麻烦的部分:

代码语言:javascript
运行
复制
@model LotusWorksHT.Models.EmployeeViewModel

<div style=" position:relative;top:40px; border-radius: 0px; border-color: #F47B20; border-style: solid; border-width: 5px; background-repeat: no-repeat; background-position: right; padding: 5px; background-size: contain; background-color:white ">
        <h2 align="center">Holiday Requests Record</h2>
        <table class="table table-striped">

            <tr>
                <th>
                    Start Date
                </th>

                <th>
                    Finish Date
                </th>
                <th>
                    HoursTaken
                </th>
                <th>
                    Comments
                </th>
                <th>
                    Year Created
                </th>

                <th>
                    Month Created
                </th>
                <th>
                    Day Created
                </th>
                <th>
                    Year Of Holiday
                </th>
            </tr>

  @foreach (var item in Model) {
<tr>

                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.StartDate)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.FinishDate)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.HoursTaken)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.Comments)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.YearCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.MonthCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.DayCreated)
                </td>
                <td>
                    @Html.DisplayFor(model => model.HolidayRequestForm.YearOfHoliday)
                </td>
            </tr>

   }
        </table>

        </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 23:43:03

尝试更改:

代码语言:javascript
运行
复制
@foreach (var item in Model)

代码语言:javascript
运行
复制
@foreach (var item in Model.HolidayRequestForm)

因为它是你想在这里遍历的HolidayRequestForm列表。然后试着改变

model.HolidayRequestForm.StartDate

代码语言:javascript
运行
复制
item.StartDate or model => model.StartDate

以获取每一处房产。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54220039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档