ASP.NET MVC Отображение данных в представлении из модели? - PullRequest
0 голосов
/ 16 января 2019

Я создал модель вида EmployeeViewModel из двух других моделей Employee и HolidayRequestForm, так как мне нужно использовать их в одном представлении. У меня проблема с отображением данных из моих моделей. Вот мои модели:

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

}

Форма для запроса праздника Модель:

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

Модель просмотра сотрудника:

 public class EmployeeViewModel
{
    public Employee Employee { get; set; }

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

Вот мое действие контроллера:

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


    }

Я пытаюсь внедрить эту модель на мой взгляд, но у меня проблемы. Вот часть вида, вызывающая проблемы:

@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>

1 Ответ

0 голосов
/ 16 января 2019

Попробуйте изменить:

@foreach (var item in Model)

до

@foreach (var item in Model.HolidayRequestForm)

В качестве списка HolidayRequestForm вы хотите просмотреть здесь. А затем попробуйте изменить

model.HolidayRequestForm.StartDate

до

item.StartDate or model => model.StartDate

Чтобы получить в каждой собственности.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...