我和这个问题有同样的问题
我有一个带有列的产品表
(PK)Product_Id, (FK)Category_Id, Name, Description, Size, Color, Quantity, Price, Condition现在,我通过一个包含所有字段的简单表单来存储这些值。
模型 Product.cs
public partial class Product
{
[Key]
public int ProductId { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[AllowHtml]
public string Description { get; set; }
public decimal? Price { get; set; }
public int? Quantity { get; set; }
public string Condition { get; set; }
[StringLength(50)]
public string Size { get; set; }
[StringLength(50)]
public string Colors { get; set; }
}视图大小和颜色通过简单复选框存储。
@using (Html.BeginForm("AddProduct", "Store", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new product.</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label", data_val_required = "required" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(m=>m.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Description, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Description, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Size, new { @class = "col-md-2 control-label sizecheckboxshow" })
<div id="sizecheckboxes" class="col-md-10">
<input type="checkbox" name="Size" value="XS" /><span class="sizecheckboxtext">XS</span>
<input type="checkbox" name="Size" value="S" /><span class="sizecheckboxtext">S</span>
<input type="checkbox" name="Size" value="M" /><span class="sizecheckboxtext">M</span>
<input type="checkbox" name="Size" value="L" /><span class="sizecheckboxtext">L</span>
<input type="checkbox" name="Size" value="XL" /><span class="sizecheckboxtext">XL</span>
<input type="checkbox" name="Size" value="XXL" /><span class="sizecheckboxtext">XXL</span>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Colors, new { @class = "col-md-2 control-label colorcheckboxshow" })
<div id="colorcheckboxes" class="col-md-10">
<input type="checkbox" name="Color" value="Red" /><span class="colorcheckboxtext">Red</span>
<input type="checkbox" name="Color" value="Green" /><span class="colorcheckboxtext">Green</span>
<input type="checkbox" name="Color" value="Blue" /><span class="colorcheckboxtext">Blue</span>
<input type="checkbox" name="Color" value="Black" /><span class="colorcheckboxtext">Black</span>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Condition, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.RadioButtonFor(x => x.Condition, "New") <text>New</text>
@Html.RadioButtonFor(x => x.Condition, "Used") <text>Used</text>
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Price, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(x => x.Price, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Quantity, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(x => x.Quantity, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Create Product" />
</div>
</div>
}控制器
使用此代码存储产品
[HttpPost]
public ActionResult AddProduct(Product newRecord)
{
newRecord.Name = Request.Form["Name"];
newRecord.CategoryId = Convert.ToInt32(Request.Form["CategoryId"]);
newRecord.Size = Request.Form["Size"];
newRecord.Colors = Request.Form["Color"];
newRecord.Description = Request.Unvalidated.Form["Description"];
newRecord.Price = Convert.ToDecimal(Request.Form["Price"]);
newRecord.Quantity = Convert.ToInt32(Request.Form["Quantity"]);
db.Products.Add(newRecord);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}该值存储在1行中。怎样才能用数量来存储尺寸?作为参考问题。
为了更好地理解这个问题,请查看这链接,当您按大小移动鼠标时,它会显示库存中的剩余项。我想要做到这一点。
发布于 2016-01-14 11:01:09
您的数据库和模型设计是不正确的,您的努力实现。您需要一个用于产品的表(例如Product),其中包含ID、Name、Description和Price等常见属性(假设价格不随大小而变化),而对于大小和StockQuantity则需要另一个表(例如“`ProductStock”)(在Products表中使用FK )。
你的模特就像
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public List<ProductStock> Stock { get; set; }
}
public class ProductStock
{
public int ID { get; set; }
public string Size { get; set; } // and enum may be better if the possible values wont change
public int Quantity { get; set; }
}那么你的看法就像
@model Product
<h2>@Html.DisplayFor(m => m.Name</h2>
@Html.DisplayFor(m => m.Description)
@Html.DisplayFor(m => m.Description)
@foreach(var item in Stock)
{
@Html.DisplayFor(m => item.Size)
@Html.DisplayFor(m => item.Quantity)
}https://stackoverflow.com/questions/34785952
复制相似问题