컨트롤러에서 여러 모델을 View로 전달

18231 단어 mvcC#

개요



Controller에서 여러 Model을 View에 전달하여 표시합니다.
이 예에서는 두 가지 모델 (Employee 및 Machine)을 표시합니다.

출력 화면





초기 상태로부터의 추가·편집 부분





Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

namespace WebApplication1.Models
{
    public class Employee
    {
        public int Id { get; set; }
        [DisplayName("Name")]
        public string Name { get; set; }
        [DisplayName("Address")]
        public string Address { get; set; }
        [DisplayName("Birthday")]
        public DateTime Birthday { get; set; }
    }
}

Machine.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

namespace WebApplication1.Models
{
    public class Machine
    {
        public int Id { get; set; }
        [DisplayName("Name")]
        public string Name { get; set; }
        [DisplayName("Manufacturer")]
        public string Manufacturer { get; set; }
        [DisplayName("Beginning of use")]
        public DateTime UseStartDate { get; set; }
    }
}

HomeViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication1.Models;

namespace WebApplication1.Models
{
    public class HomeViewModel
    {
        public IEnumerable<Employee> Employees { get; set; }
        public IEnumerable<Machine> Machines { get; set; }
    }
}

HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomeViewModel myView = new HomeViewModel();
            myView.Employees = GetEmployee();
            myView.Machines = GetMachines();
            return View(myView);
        }
        private List<Employee> GetEmployee()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { Id = 1, Name = "Alex", Address = "NewYork", Birthday = new DateTime(1985, 1, 1) });
            employees.Add(new Employee { Id = 2, Name = "Ben", Address = "London", Birthday = new DateTime(1989, 6, 13) });
            employees.Add(new Employee { Id = 3, Name = "Chris", Address = "Tokyo", Birthday = new DateTime(1993, 11, 14) });
            return employees;
        }
        private List<Machine> GetMachines()
        {
            List<Machine> machines = new List<Machine>();
            machines.Add(new Machine { Id = 1, Name = "My favorite machine", Manufacturer = "Constructor Inc", UseStartDate = new DateTime(1978, 8, 19) });
            return machines;
        }
    }
}

Index.cshtml
@model WebApplication1.Models.HomeViewModel
@{
    ViewBag.Title = "Home Page";
}

<div class="row">
    <div class="col-md-6">
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Employees.First().Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Employees.First().Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Employees.First().Address)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Employees.First().Birthday)
                </th>
            </tr>
            @foreach (var item in Model.Employees)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Address)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Birthday)
                    </td>
                </tr>
            }
        </table>
    </div>
    <div class="col-md-6">
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Machines.First().Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Machines.First().Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Machines.First().Manufacturer)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Machines.First().UseStartDate)
                </th>
            </tr>
            @foreach (var item in Model.Machines)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Manufacturer)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.UseStartDate)
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

참고



아래 기사에서 여러 Model을 View에 전달하는 방법이 6가지 소개되었습니다.
Multiple Models in Single View in MVC

게다가 이 기사의 게시자는

Which if these would be considered best practice?

라는 질문에

My choice is always ViewModel

라고 대답하고 있었으므로, 이 방법을 채용했습니다.

좋은 웹페이지 즐겨찾기