Asp.Net存储产品属性(大小,数量)

时间:2016-01-14 09:32:35

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

我遇到与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链接以了解此问题,当您在尺寸上移动鼠标时,它会显示库存中的左侧项目。我想实现这一目标。

1 个答案:

答案 0 :(得分:3)

您的数据库和模型设计不符合您的目标。您需要一个产品表(例如Product),包含常见属性,如ID,名称,描述和价格(假设价格不随大小而变化),另一个表(比如`ProductStock)大小和StockQuantity(产品表格中有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)
}