简体   繁体   English

在数据库Asp.Net MVC中插入值

[英]Inserting values in database Asp.Net MVC

I have a Product table with columns Product_Id(pk), Name, Price, description, CategoryId, SubCategoryId, SubSubCategoryId 我有一个列的产品Product_Id(pk), Name, Price, description, CategoryId, SubCategoryId, SubSubCategoryId

and another Category table with columns CategoryId(pk), CategoryName, ParentId . 以及另一个带有CategoryId(pk), CategoryName, ParentId列的Category表。

and another table ProductAttributesValues with columns ProductAttributeValueId(pk), ProductId(fk), Size, Quantity 以及另一个具有列ProductAttributeValueId(pk), ProductId(fk), Size, QuantityProductAttributesValues

I've this data in my category table: 我的类别表中有这些数据:

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

As you can see Category with NULL value is Parent and other are their child and so on. 如您所见,具有NULL值的Category为Parent,其他为子级,依此类推。 Like: Category Clothing > SubCategory Men's > SubSubCategory Shirts 像: 类别服装> 子类别男士> SubSubCategory衬衫

Product.cs 产品.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 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; }
}

ViewModel 视图模型

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; }
}

Controller 控制者

[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");
     }

View 视图

@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>
   }

PROBLEM 问题

All values from fields are inserted successfully in both Product and ProductAttributesValues tables but only SubCategoryId and SubSubCategoryId is NULL in product table. 来自字段的所有值都成功插入到Product和ProductAttributesValues表中,但是在product表中只有SubCategoryId和SubSubCategoryId为NULL。 I've created these subcategory and subsubcategor columns in Product table so I can perform Searching through this. 我已经在“产品”表中创建了这些子类别和子类别列,因此可以执行“搜索”。

I've populated 3 dropdownlist for category, subcategory and subsubcategory, When I select any category, CategoryId is popedup using this 我已经为类别,子类别和子子类别填充了3个下拉列表,当我选择任何类别时,使用此类别会弹出CategoryId

<script type="text/javascript">
function jsFunction(value) {
    alert(value);
}

but not storing subcategoryId and subsubcategoryId value in product table. 但不在产品表中存储subcategoryId和subsubcategoryId值。 All remaining fields are stored except these two. 除这两个字段外,所有剩余字段均被存储。

This is because you used the same SELECT HTML tag element for both of them. 这是因为您对它们两个都使用了相同的SELECT HTML标记元素。

The problem is with @Html.DropDownList("CategoryId" should be different for both of them. 问题在于@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>

Should be: 应该:

<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>

First generate the select list for subcategory and subsubcategory from controller : 首先从控制器生成子类别和子子类别的选择列表:

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 });

Then follow the answer of @Orel Eraki. 然后按照@Orel Eraki的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM