如何在 ASP.NET MVC 5 的视图中将 ViewModel 拆分为两个列表
How to split a ViewModel into two lists on a view in ASP.NET MVC 5
所以我的产品及其相关选项有这个视图模型,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CuttingEdgeOrderingSystem.Models
{
public class ProductOptionVM
{
public string ProductName { get; set; }
public string ImageScr { get; set; }
public string Desc { get; set; }
public List<OptionsVM> Options { get; set; }
public int ProductId { get; set;}
}
public class OptionsVM
{
public string OptionName { get; set; }
public string Price { get; set; }
public int OptionsId { get; set; }
}
}
而且我想单独显示产品,并在其下方列出其选项,每个选项都有一个单选按钮。
这是我当前的查看代码。我将产品作为列表生产,无法弄清楚如何让每个选项显示在此结构中的列表中。感谢任何帮助。
@model IEnumerable<CuttingEdgeOrderingSystem.Models.ProductOptionVM>
@{
ViewBag.Title = "Shop";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Products</h2>
<div class="row">
@foreach (var product in Model)
{
<!-- Panel to display products-->
<div class="col-md-4 col-sm-12 panel-height">
<div class="panel panel-default">
<span class="panel-title">@Html.DisplayFor(modelItem => product.ProductName)</span>
<div class="">
<img src="@Html.DisplayFor(modelItem => product.ImageScr)" class="panel-image" />
</div>
<div class="panel-default text-black panel-border">
@Html.DisplayFor(modelItem => product.Desc)
</div>
<div class="panel-default text-black">
<!-- options for products go here -->
</div>
<div class="panel-default text-black">
<button type="button" class="btn btn-primary btn-sm">Add to Cart</button>
</div>
</div>
</div>
}
</div>
您可以像这样在每个选项上调用 DisplayFor:
<div class="panel-default text-black">
@for(var i = 0; i < product.Options.Count(); i++)
{
@Html.DisplayFor(o => product.Options[i])
}
</div>
不确定 DisplayFor 在你的情况下看起来有多好,但我想你明白了。
所以我的产品及其相关选项有这个视图模型,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CuttingEdgeOrderingSystem.Models
{
public class ProductOptionVM
{
public string ProductName { get; set; }
public string ImageScr { get; set; }
public string Desc { get; set; }
public List<OptionsVM> Options { get; set; }
public int ProductId { get; set;}
}
public class OptionsVM
{
public string OptionName { get; set; }
public string Price { get; set; }
public int OptionsId { get; set; }
}
}
而且我想单独显示产品,并在其下方列出其选项,每个选项都有一个单选按钮。
这是我当前的查看代码。我将产品作为列表生产,无法弄清楚如何让每个选项显示在此结构中的列表中。感谢任何帮助。
@model IEnumerable<CuttingEdgeOrderingSystem.Models.ProductOptionVM>
@{
ViewBag.Title = "Shop";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Products</h2>
<div class="row">
@foreach (var product in Model)
{
<!-- Panel to display products-->
<div class="col-md-4 col-sm-12 panel-height">
<div class="panel panel-default">
<span class="panel-title">@Html.DisplayFor(modelItem => product.ProductName)</span>
<div class="">
<img src="@Html.DisplayFor(modelItem => product.ImageScr)" class="panel-image" />
</div>
<div class="panel-default text-black panel-border">
@Html.DisplayFor(modelItem => product.Desc)
</div>
<div class="panel-default text-black">
<!-- options for products go here -->
</div>
<div class="panel-default text-black">
<button type="button" class="btn btn-primary btn-sm">Add to Cart</button>
</div>
</div>
</div>
}
</div>
您可以像这样在每个选项上调用 DisplayFor:
<div class="panel-default text-black">
@for(var i = 0; i < product.Options.Count(); i++)
{
@Html.DisplayFor(o => product.Options[i])
}
</div>
不确定 DisplayFor 在你的情况下看起来有多好,但我想你明白了。