【发布时间】:2016-08-30 20:37:59
【问题描述】:
我有一个可枚举模型的视图。我正在尝试将文本框与对象实例的值绑定。这是我的看法。
@model IEnumerable<SeniorProjectMVC.Models.Cart>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Your cart</h3>
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
<div class="row">
<div class="large-2 columns">
<h4> @Html.DisplayNameFor(model => model.Quantity)</h4>
</div>
<div class="large-4 columns">
<h4>@Html.DisplayNameFor(model => model.Sku.Product.Name)</h4>
</div>
<div class="large-2 columns">
<h4>@Html.DisplayNameFor(model => model.Sku.Product.Price)</h4>
</div>
<div class="large-1 columns">
<h4>In stock</h4>
</div>
<div class="large-3 columns">
</div>
</div>
@foreach (var item in Model)
{
<div class="row">
<div class="large-2 columns">
@Html.TextBox(item.Quantity.ToString())
</div>
<div class="large-4 columns">
@Html.Display(item.Sku.Product.Name)
</div>
<div class="large-2 columns">
@(item.Sku.Product.Price.ToString("c"))
</div>
<div class="large-1 columns">
@(item.Sku.Quantity > 0 ? "In Stock" : "Out of stock")
</div>
<div class="large-3 columns">
@Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class = "button primary" }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }, new { @class = "button primary" }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "button primary" })
</div>
</div>
}
我似乎遇到的问题是数量不会填充在文本框中,并且产品名称似乎也没有出现。我知道产品名称有值,但它们没有显示。此外,如果我想在值更改的情况下发回此集合,那么使用相应 ID 发回所有文本框值的正确方法是什么,以便我可以更新购物车中已更改的数量。
【问题讨论】:
-
您确定每个项目在
Quantity属性中都有有效值吗? -
是的,我可以发张照片。价格似乎绑定正确,但数量没有填充到文本框中,每个产品的名称也没有显示。
-
从 dbContext 读取时可能需要使用
Include。 -
你能展示一个包含的例子吗?
标签: c# asp.net-mvc asp.net-mvc-4 razor ef-code-first