【发布时间】:2017-07-04 00:32:11
【问题描述】:
我正在做一个简单的项目。以下是代码。问题是我无法从视图中获取所有信息到控制器。所以我已经在 Homecontroller 中拥有书籍 ID、名称和价格(来自单独的 Productcontroller),我也可以查看它,并且我想在发出发布请求时发布书籍的数量。因此,当我尝试发出发布请求并对其进行调试时,我只能看到数量,并且我看到图书 ID、名称和价格为空。
我不确定出了什么问题。任何帮助将非常感激。 谢谢!
1.Product.cs
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal ProductPrice { get; set; }
public int Quantity { get; set; }
}
2.Cart.cshtml
@model DAL.Models.Product
@{
ViewBag.Title = "Purchase";
}
@ViewBag.bookName
<h2>Purchase</h2>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
@using (Html.BeginForm("Cart", "Home", FormMethod.Post))
{
<div class="panel panel-primary">
<div class="panel-heading">Purchase Book</div>
<div class="panel-body">
<div>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.ProductName)
</dt>
<dd>
@Html.DisplayFor(model => model.ProductName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ProductPrice)
</dt>
<dd>
@Html.DisplayFor(model => model.ProductPrice)
</dd>
</dl>
</div>
@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</div><br />
<div class="col-md-10">
<input type="submit" value="Submit" class="btn btn-default"/>
</div>
</div>
</div>
}
</div>
<div class="col-md-3"></div>
</div>
以下是屏幕截图:
【问题讨论】: