我有一个产品表,其中包含Product_Id(pk), Name, Price, description, CategoryId, SubCategoryId, SubSubCategoryId
列
以及另一个类别表,其中包含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
值的类别是父级,其他是他们的孩子,依此类推。喜欢:类别服装> 子类别男士> 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表中创建了这些子类别和subsubcategor列,因此我可以执行搜索。
我已经为类别,子类别和子子类别填充了3个下拉列表,当我选择任何类别时,CategoryId是使用此类的popedup
<script type="text/javascript">
function jsFunction(value) {
alert(value);
}
但不在产品表中存储subcategoryId和subsubcategoryId值。除了这两个以外,所有剩余的字段都会被存储。
答案 0 :(得分:1)
这是因为您对它们使用了相同的@Html.DropDownList("CategoryId"
HTML标记元素。
<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.SubCategoryId, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("SubCategoryId", 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("SubSubCategoryId", null, String.Empty, new { @class = "col-xs-8 control-label CssSubSubCategory" })
</div>
</div>
应该是:
{{1}}
答案 1 :(得分:1)
首先从控制器生成子类别和子子类别的选择列表:
ViewBag.SubCategoryId= db.Categories.Where(w => w.ParentId == null).Select(c => new SelectListItem
{
Value = c.SubCategoryId.ToString(),
Text = c.Name
});
ViewBag.SubSubCategoryId= db.Categories.Where(w => w.ParentId == null).Select(c => new SelectListItem
{
Value = c.SubSubCategoryId.ToString(),
Text = c.Name
});
然后按照@Orel Eraki的回答。