디스플레이 템플릿을 사용할 수 있습니다. 예를 들어 봅시다.
모델 :
public class Car
{
public string CommonProperty { get; set; }
}
public class SportCar : Car
{
public string CarSpecificProperty { get; set; }
}
public class Truck: Car
{
public string TruckSpecificProperty { get; set; }
}
컨트롤러 :
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new Car[]
{
new SportCar { CommonProperty = "sports car common", CarSpecificProperty = "car specific" },
new Truck { CommonProperty = "truck common", TruckSpecificProperty = "truck specific" },
};
}
}
보기 (~/Views/Home/Index.cshtml
) : 스포츠 자동차에 대한
@model Car[]
@for (int i = 0; i < Model.Length; i++)
{
<div>
@Html.DisplayFor(x => x[i].CommonProperty)
@Html.DisplayFor(x => x[i])
</div>
}
디스플레이 템플릿 (~/Views/Home/DisplayTemplates/SportCar.cshtml
) :
트럭에 대한
디스플레이 템플릿 (~/Views/Home/DisplayTemplates/Truck.cshtml
) : 답안에 대한
@model Truck
@Html.DisplayFor(x => x.TruckSpecificProperty)
감사합니다! 나는 그것을 시도 할 것이다! – Ron83