【发布时间】:2016-01-14 09:32:34
【问题描述】:
我遇到了和this question一样的问题
我有一个带有列的产品表
(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 行中。如何存储带有数量的尺寸?作为引用的问题。
请查看this 链接以进一步了解此问题,当您在尺寸上移动鼠标时,它会显示剩余的库存商品。我想实现这一点。
【问题讨论】:
-
为什么在已有模型的情况下使用
Request.Form[..]来获取值?你是什么型号?您试图在Size和Color字段(以逗号分隔的值列表)中存储什么? -
为什么?您应该有一张用于产品(ID、描述、价格等)的表格和一张用于尺寸和相关库存数量的表格(不确定颜色适合它的位置)
-
我处于学习阶段,但我得到的结果用逗号分隔,我知道必须有很多其他方法来存储这些值。只需删除颜色属性,我如何存储数量众多的尺寸?在问题结束时,请检查该链接,然后将鼠标移至 SIZES,它会显示左侧项目。我想这样做。
-
那么你需要重新考虑你的数据库设计。目前没有时间,但如果没有其他人,我稍后会添加答案。
-
@StephenMuecke 我应该再制作一张尺码表吗(现在让我们只掉色)?尺寸标识、尺寸名称。但我仍然对存储数量感到困惑。假设我有一件 S、M、L 尺寸和数量为 S=10、M=5 和 L=20 的衬衫,我将在哪里存储这些值以及如何存储?
标签: c# mysql asp.net asp.net-mvc asp.net-mvc-4