컨트롤러에서 여러 모델을 View로 전달
개요
Controller에서 여러 Model을 View에 전달하여 표시합니다.
이 예에서는 두 가지 모델 (Employee 및 Machine)을 표시합니다.
출력 화면
초기 상태로부터의 추가·편집 부분
Employee.csusing 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.csusing 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.csusing 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.csusing 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
라고 대답하고 있었으므로, 이 방법을 채용했습니다.
Reference
이 문제에 관하여(컨트롤러에서 여러 모델을 View로 전달), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/KktkiY/items/f28528916e97310262e0
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
초기 상태로부터의 추가·편집 부분
Employee.csusing 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.csusing 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.csusing 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.csusing 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
라고 대답하고 있었으므로, 이 방법을 채용했습니다.
Reference
이 문제에 관하여(컨트롤러에서 여러 모델을 View로 전달), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/KktkiY/items/f28528916e97310262e0
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
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; }
}
}
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; }
}
}
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; }
}
}
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;
}
}
}
@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
라고 대답하고 있었으므로, 이 방법을 채용했습니다.
Reference
이 문제에 관하여(컨트롤러에서 여러 모델을 View로 전달), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/KktkiY/items/f28528916e97310262e0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)