我有作爲this questionAsp.Net店產品屬性(尺寸,數量)
我有一個產品表列
(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鏈接以更好地瞭解此問題,當您在尺寸上移動鼠標時,它會顯示庫存中的左側物品。我想實現這一點。
有一個模型?你在做什麼?你想在「尺寸」和「顏色」字段中存儲什麼(逗號分隔的值列表)? –
爲什麼?你應該有一個產品的表格(ID,描述,價格等)和一個表格的大小和相關的庫存數量(不知道顏色適合於它) –
我處於學習階段,但我得到以逗號分隔的結果,我知道必須有許多其他方式來存儲這些值。只需刪除顏色屬性,我如何存儲多個數量的尺寸?在問題結束時,請檢查該鏈接,並將鼠標移動到SIZES,它會顯示左邊的項目。我想做這個。 – user3223395667