[英]Inserting values in database Asp.Net MVC
我有一個列的產品表Product_Id(pk), Name, Price, description, CategoryId, SubCategoryId, SubSubCategoryId
以及另一個帶有CategoryId(pk), CategoryName, ParentId
列的Category表。
以及另一個具有列ProductAttributeValueId(pk), ProductId(fk), Size, Quantity
表ProductAttributesValues
我的類別表中有這些數據:
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為Parent,其他為子級,依此類推。 像: 類別服裝> 子類別男士> SubSubCategory襯衫
產品.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表中,但是在product表中只有SubCategoryId和SubSubCategoryId為NULL。 我已經在“產品”表中創建了這些子類別和子類別列,因此可以執行“搜索”。
我已經為類別,子類別和子子類別填充了3個下拉列表,當我選擇任何類別時,使用此類別會彈出CategoryId
<script type="text/javascript">
function jsFunction(value) {
alert(value);
}
但不在產品表中存儲subcategoryId和subsubcategoryId值。 除這兩個字段外,所有剩余字段均被存儲。
這是因為您對它們兩個都使用了相同的SELECT
HTML標記元素。
問題在於@Html.DropDownList("CategoryId"
對於他們兩個應該是不同的。
<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>
首先從控制器生成子類別和子子類別的選擇列表:
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的回答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.