【发布时间】:2016-01-24 14:46:17
【问题描述】:
我有一个 Product 表,其中包含 Product_Id(pk), Name, Price, description, CategoryId, SubCategoryId, SubSubCategoryId
还有另一个 Category 表,其中包含 CategoryId(pk), CategoryName, ParentId 列。
和另一个表 ProductAttributesValues 与列 ProductAttributeValueId(pk), ProductId(fk), Size, Quantity
我的类别表中有这些数据:
CategoryId | CategoryName | ParentId
1 | Clothing | NULL
2 | Computing | NULL
3 | Books | NULL
4 | Men's | 1
5 | Women's | 1
6 | Kid's | 1
7 | Laptops | 2
8 | Desktops | 2
9 | Shirts | 4
10 | Tops | 5
11 | Macbooks | 7
11 | Ultrabooks | 7
如您所见,具有NULL 值的类别是父级,其他是他们的子级,依此类推。喜欢:Category 服装 > SubCategory 男装 > SubSubCategory 衬衫
Product.cs
public partial class Product
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubCategoryId { get; set; }
public int SubSubCategoryId { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[AllowHtml]
public string Description { get; set; }
public decimal? Price { get; set; }
}
ProductAttributeValues.cs
public partial class ProductAttributeValues
{
public int ProductAttributeValueId { get; set; }
public int ProductId { get; set; }
public string Size {get; set; }
public int Quantity { get; set; }
}
视图模型
public class ProductAttributesViewModel
{
public int ProductId { get; set; }
public int? CategoryId { get; set; }
public int? SubCategoryId { get; set; }
public int? SubSubCategoryId { 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 Sizes { get; set; }
public int Stock { get; set; }
public Product Product { get; set; }
public Category Category { get; set; }
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddProduct(ProductAttributesViewModel newRecord)
{
IEnumerable<SelectListItem> categories = db.Categories.Where(w => w.ParentId == null).Select(c => new SelectListItem
{
Value = c.CategoryId.ToString(),
Text = c.Name
});
ViewBag.CategoryId = categories;
var product = new Product
{
Name = newRecord.Name,
CategoryId = newRecord.CategoryId,
SubCategoryId = newRecord.SubCategoryId,
SubSubCategoryId = newRecord.SubSubCategoryId,
Description = newRecord.Description,
Price = newRecord.Price,
Quantity = newRecord.Quantity
};
var productattributevalues = new ProductAttributeValue
{
ProductId = newRecord.ProductId,
Size = newRecord.Size
Quantity = newRecord.Stock
};
db.Products.Add(product);
db.ProductAttributeValues.Add(productattributevalues);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
查看
@model ProjectABC.ViewModels.ProductAttributesViewModel
@using (Html.BeginForm("AddProduct", "Store", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal col-md-12", role = "form" }))
{
<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" })
</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.CategoryId, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, String.Empty, new {onchange="jsFunction(this.value);", @class = "col-xs-8 control-label CssCategory" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.SubCategoryId, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, String.Empty, new { @class = "col-xs-8 control-label" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.SubSubCategoryId, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, String.Empty, new { @class = "col-xs-8 control-label CssSubSubCategory" })
</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>
}
问题
字段中的所有值都已成功插入到 Product 和 ProductAttributesValues 表中,但只有 SubCategoryId 和 SubSubCategoryId 在产品表中为 NULL。我已经在 Product 表中创建了这些子类别和子子类别列,因此我可以通过它执行搜索。
我已经为类别、子类别和子子类别填充了 3 个下拉列表,当我选择任何类别时,CategoryId 使用此弹出
<script type="text/javascript">
function jsFunction(value) {
alert(value);
}
但不在产品表中存储 subcategoryId 和 subsubcategoryId 值。除了这两个之外,所有剩余的字段都被存储。
【问题讨论】:
-
我已经发布了一个可能的答案。
-
你检查过
newRecord吗?它是否包含SubCategoryId和SubSubCategoryId的任何值? -
@MahbuburRahman 为空。但是当我从下拉列表中更改值时,我通过脚本获得了警报框中的 ID
-
您会得到,因为您同时使用了子类别和子子类别的类别下拉列表。
标签: c# asp.net asp.net-mvc asp.net-mvc-4