【发布时间】:2019-11-27 19:22:29
【问题描述】:
所以我有一个名为 Item 的父类:
public class Item {
public Guid Id {get; set;}
public double Price {get; set;}
}
Bread 和 Cookie 类继承自该类:
public class Bread : Item {
public string Type {get; set;}
}
public class Cookie : Item {
public string Flavour {get; set;}
public string Size {get; set;}
}
我在带有“AddToCart”按钮的 Razor 视图的表格中显示这些内容。
@using Data.Entities
@model ViewModels.CookieIndexViewModel
@{
ViewBag.Title = "Cookie list";
}
<h1>Our collection of cookies:</h1>
<table class="table">
@foreach (var cookie in Model.Cookies)
{
<tr>
<td>@cookie.Flavour</td>
<td>@cookie.Size</td>
<td>@cookie.Price</td>
<td><a class="btn btn-primary text-white" asp-action="AddItem" asp-controller="Cart" asp-route-item="@cookie">Purchase</a></td>
</tr>
}
</table>
在 CartController 中,我有 AddItem 方法,它以 Item 作为参数
public IActionResult AddItem(Item item)
{
return View();
}
但这不起作用,因为 Cookie 无法转换为 Item 有什么提示可以做到这一点吗?
【问题讨论】:
-
我在您的父类“Item”或子类“Cookie”中都看不到任何“Size”属性。您希望它如何出现在视图中?
-
哦,那个在 cookie 类中,我忘了添加那个。我现在编辑它。
标签: asp.net-mvc polymorphism tag-helpers