gpt4 book ai didi

c# - Asp.Net 商店产品属性(尺寸、数量)

转载 作者:行者123 更新时间:2023-11-29 05:14:38 25 4
gpt4 key购买 nike

我遇到了与 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; }
}

View 尺寸和颜色由简单的复选框存储。

@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>
}

Controller

使用此代码存储产品

[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链接也可以更好地理解这个问题,当您将鼠标移到尺码上时,它会显示剩余的库存商品。我想实现这个。

最佳答案

您的数据库和模型设计不适合您要实现的目标。您需要一个产品表(例如 Product),其中包含通用属性,例如 ID、名称、描述和价格(假设价格不随尺寸变化),以及另一个表(例如“ProductStock ) 用于 Size 和 StockQuantity(使用 FK 到 Products 表)。

你的模型应该是这样的

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; }
}

那么你的 View 会是这样的

@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)
}

关于c# - Asp.Net 商店产品属性(尺寸、数量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com