I have 4 tables

Product(id,name,catid,subcatid,subsubcatid)

Category(catid,catname)

SubCategory(subcatid,subcatname,catid)

SubSubCategory(subsubcatid,subsubcatname,subcatid)

I want to add new product. First I was adding product by entering only product name and selecting category from category dropdownlist, it was working, then I added subcategory dropdownlist and it was working too but when I added subsubcategory dropdown table and dropdownlist it is giving me this error

Invalid column name 'SubSubCategory_SubSubCatId'.

[HttpGet] method is working fine. I get the list of subsubcategories but when i add product the post method gives the above error at "db.SaveChanges()" line

Controller

[HttpGet]
    public ActionResult AddProduct(string id)
    {
        IEnumerable<SelectListItem> categories = db.Categories.Select(c => new SelectListItem
        {
            Value = c.CategoryId.ToString(),
            Text = c.Name

        });
        ViewBag.CategoryId = categories;

        IEnumerable<SelectListItem> subcategories = db.SubCategories.Select(c => new SelectListItem
        {
            Value = c.SubCatId.ToString(),
            Text = c.SubCatName

        });
        ViewBag.SubCategoryId = subcategories;

        IEnumerable<SelectListItem> subsubcategories = db.SubSubCategories.Select(c => new SelectListItem
        {
            Value = c.SubSubCatId.ToString(),
            Text = c.SubSubCatName

        });
        ViewBag.SubSubCategoryId = subsubcategories;
    return View();
    }

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AddProduct(HttpPostedFileBase file)
    {
      IEnumerable<SelectListItem> categories = db.Categories.Select(c => new SelectListItem
        {
            Value = c.CategoryId.ToString(),
            Text = c.Name

        });

        ViewBag.CategoryId = categories;

        IEnumerable<SelectListItem> subcategories = db.SubCategories.Select(c => new SelectListItem
        {
            Value = c.SubCatId.ToString(),
            Text = c.SubCatName

        });
        ViewBag.SubCategoryId = subcategories;

        IEnumerable<SelectListItem> subsubcategories = db.SubSubCategories.Select(c => new SelectListItem
        {
            Value = c.SubSubCatId.ToString(),
            Text = c.SubSubCatName

        });
        ViewBag.SubSubCategoryId = subsubcategories;


                Product newRecord = new Product();
                newRecord.Name = Request.Form["Name"];
                newRecord.CategoryId = Convert.ToInt32(Request.Form["CategoryId"]);
                newRecord.SubCategoryId = Convert.ToInt32(Request.Form["SubCategoryId"]);
                newRecord.SubSubCategoryId = Convert.ToInt32(Request.Form["SubSubCategoryId"]);
                db.Products.Add(newRecord);
                db.SaveChanges();
                return RedirectToAction("Index","Home");


        return View();
    }

View

<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.CategoryId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.CategoryId, ViewBag.CategoryId as SelectList, new { @class = "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.DropDownListFor(x => x.SubCategoryId, ViewBag.SubCategoryId as SelectList, new { @class = "CssSubCategory" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.SubSubCategoryId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(x => x.SubSubCategoryId, ViewBag.SubSubCategoryId as SelectList, new { @class = "CssSubSubCategory" })
    </div>
</div>

Related posts

Recent Viewed