【问题标题】:Asp.Net store product attributes (Size,Quantity)Asp.Net 商店产品属性(尺寸、数量)
【发布时间】: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[..] 来获取值?你是什​​么型号?您试图在 SizeColor 字段(以逗号分隔的值列表)中存储什么?
  • 为什么?您应该有一张用于产品(ID、描述、价格等)的表格和一张用于尺寸和相关库存数量的表格(不确定颜色适合它的位置)
  • 我处于学习阶段,但我得到的结果用逗号分隔,我知道必须有很多其他方法来存储这些值。只需删除颜色属性,我如何存储数量众多的尺寸?在问题结束时,请检查该链接,然后将鼠标移至 SIZES,它会显示左侧项目。我想这样做。
  • 那么你需要重新考虑你的数据库设计。目前没有时间,但如果没有其他人,我稍后会添加答案。
  • @StephenMuecke 我应该再制作一张尺码表吗(现在让我们只掉色)?尺寸标识、尺寸名称。但我仍然对存储数量感到困惑。假设我有一件 S、M、L 尺寸和数量为 S=10、M=5 和 L=20 的衬衫,我将在哪里存储这些值以及如何存储?

标签: c# mysql asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您的数据库和模型设计不适合您想要实现的目标。您需要一个产品表(例如Product),其中包含 ID、名称、描述和价格等常见属性(假设价格不随尺寸而变化),以及另一个表(例如“ProductStock”)用于尺寸和StockQuantity(带有 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)
}

【讨论】:

  • 感谢您的回答,但如何使用控制器进行存储?然后显示在productDetails View页面中。
  • 这个答案太多了。对于创建/编辑产品,您可以使用答案herehere 中的技术来动态添加尺寸/数量行,并将产品及其库存集合保存在一个表单/控制器中。
  • 为了显示产品,我展示了一个简化视图(显然,您链接到的站点正在生成带有边框等的 &lt;span&gt; 元素和一个 title 属性以在工具提示中显示可用数量) .我不知道您是否使用 EF 或其他存储库代码,但它的控制器可能类似于 public ActionResult Details(int ID) { Product model = db.Products.Include("Stock").Where(p =&gt; p.ID == ID).FirstOrDefault(); return View(model); }
  • 在保存产品按钮上我需要在两个表中存储产品? Products & ProductsStock 然后显示在 productdetails 页面?
  • 是的 - 请参阅 this answer 以获取 EF 中的示例。另请注意,您当前的设计过于僵化-如果从集合中添加(或删除)新尺寸(或颜色)会发生什么情况-您已将其硬编码到视图中。这是您需要第二个表以及添加删除库存项目以及更改数量的另一个原因。
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多