I am working on a project where i have three tables ProductType , ProductSubType and Product . what i want to do is when i select productType in product createview then it populates the ProductSubType accordingly. I want to apply this functionality in the
Create and Edit view of Product Table . I have tried to look for the solution but didnt find any , Please if someone can help me Many Thanks
My Tables
Product Type :
public class ProductTypeModels
{
[Key]
public int TypeID { get; set; }
public string TypeName { get; set; }
}
ProductSubType Table ;
public class ProductSubTypeModels
{
[Key]
public int SubTypeID { get; set; }
public int TypeID { get; set; }
public string SubTypeName { get; set; }
public List<ProductModels> Products { get; set; }
public virtual ProductTypeModels ProductTypes { get; set; }
}
Product Table :
public class ProductModels
{
[Key]
public int ProductID { get; set; }
public int TypeID { get; set; }
public int SubTypeID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string SKU { get; set; }
public decimal UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public virtual ProductTypeModels ProductTypes { get; set; }
public virtual ProductSubTypeModels ProductSubTypes { get; set; }
public List<ProductTypeModels> productTypes { get; set; }
public List<ProductSubTypeModels> productSubTypes { get; set; }
}
Controller for Product
public class ProductsController : Controller
{
private DataContext db = new DataContext();
//
// GET: /Products/Create
public ActionResult Create()
{
ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName");
ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName");
return View();
}
//
// POST: /Products/Create
[HttpPost]
public ActionResult Create(ProductModels productmodels, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
db.Products.Add(productmodels);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID);
ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID);
return View(productmodels);
}
//
// GET: /Products/Edit/5
public ActionResult Edit(int id = 0)
{
ProductModels productmodels = db.Products.Find(id);
if (productmodels == null)
{
return HttpNotFound();
}
ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID);
ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID);
return View(productmodels);
}
//
// POST: /Products/Edit/5
[HttpPost]
public ActionResult Edit(ProductModels productmodels)
{
if (ModelState.IsValid)
{
db.Entry(productmodels).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID);
ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID);
return View(productmodels);
}
//
// GET: /Products/Delete/5
public ActionResult Delete(int id = 0)
{
ProductModels productmodels = db.Products.Find(id);
if (productmodels == null)
{
return HttpNotFound();
}
return View(productmodels);
}
//
// POST: /Products/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
ProductModels productmodels = db.Products.Find(id);
db.Products.Remove(productmodels);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
rlondonboy
Member
2 Points
29 Posts
Dropdown list cascade in mvc3.0
Oct 26, 2012 03:47 PM|LINK
I am working on a project where i have three tables ProductType , ProductSubType and Product . what i want to do is when i select productType in product createview then it populates the ProductSubType accordingly. I want to apply this functionality in the Create and Edit view of Product Table . I have tried to look for the solution but didnt find any , Please if someone can help me Many Thanks
My Tables Product Type : public class ProductTypeModels { [Key] public int TypeID { get; set; } public string TypeName { get; set; } } ProductSubType Table ; public class ProductSubTypeModels { [Key] public int SubTypeID { get; set; } public int TypeID { get; set; } public string SubTypeName { get; set; } public List<ProductModels> Products { get; set; } public virtual ProductTypeModels ProductTypes { get; set; } } Product Table : public class ProductModels { [Key] public int ProductID { get; set; } public int TypeID { get; set; } public int SubTypeID { get; set; } public string Title { get; set; } public string Description { get; set; } public string SKU { get; set; } public decimal UnitPrice { get; set; } public int UnitsInStock { get; set; } public virtual ProductTypeModels ProductTypes { get; set; } public virtual ProductSubTypeModels ProductSubTypes { get; set; } public List<ProductTypeModels> productTypes { get; set; } public List<ProductSubTypeModels> productSubTypes { get; set; } } Controller for Product public class ProductsController : Controller { private DataContext db = new DataContext(); // // GET: /Products/Create public ActionResult Create() { ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName"); ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName"); return View(); } // // POST: /Products/Create [HttpPost] public ActionResult Create(ProductModels productmodels, HttpPostedFileBase file) { if (ModelState.IsValid) { db.Products.Add(productmodels); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID); ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID); return View(productmodels); } // // GET: /Products/Edit/5 public ActionResult Edit(int id = 0) { ProductModels productmodels = db.Products.Find(id); if (productmodels == null) { return HttpNotFound(); } ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID); ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID); return View(productmodels); } // // POST: /Products/Edit/5 [HttpPost] public ActionResult Edit(ProductModels productmodels) { if (ModelState.IsValid) { db.Entry(productmodels).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.TypeID = new SelectList(db.ProductTypes, "TypeID", "TypeName", productmodels.TypeID); ViewBag.SubTypeID = new SelectList(db.ProductSubTypes, "SubTypeID", "SubTypeName", productmodels.SubTypeID); return View(productmodels); } // // GET: /Products/Delete/5 public ActionResult Delete(int id = 0) { ProductModels productmodels = db.Products.Find(id); if (productmodels == null) { return HttpNotFound(); } return View(productmodels); } // // POST: /Products/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { ProductModels productmodels = db.Products.Find(id); db.Products.Remove(productmodels); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } }ignatandrei
All-Star
135204 Points
21687 Posts
Moderator
MVP
Re: Dropdown list cascade in mvc3.0
Oct 26, 2012 04:08 PM|LINK
cascade dropdown. Please see http://bit.ly/mvc_ajax_jquery
rlondonboy
Member
2 Points
29 Posts
Re: Dropdown list cascade in mvc3.0
Oct 26, 2012 04:36 PM|LINK
Hi I m following the thread by shashank_mehta
http://forums.asp.net/t/1847893.aspx/1?how+to+bind+sub+category+in+dropdownlist+mvc4
I am applying this concept to Create view page and Product. controller class but its giving me error
{"More than one item in the metadata collection match the identity 'DrinkHouse.Models.ProductModels_ProductTypes'."}
ignatandrei
All-Star
135204 Points
21687 Posts
Moderator
MVP
Re: Dropdown list cascade in mvc3.0
Oct 27, 2012 03:38 AM|LINK
If you are following a tutorial, ask the original author first. If the author does not answer, seek other tutorial...
CPrakash82
All-Star
18314 Points
2851 Posts
Re: Dropdown list cascade in mvc3.0
Oct 29, 2012 02:20 AM|LINK
You should refer the @Rick's tutorial on this.
rlondonboy
Member
2 Points
29 Posts
Re: Dropdown list cascade in mvc3.0
Nov 05, 2012 08:37 PM|LINK
Thanks for your answers .
Nandip Makwa...
Participant
1267 Points
293 Posts
Re: Dropdown list cascade in mvc3.0
Nov 06, 2012 07:50 AM|LINK
Have you checked my this post http://www.dotnetexpertguide.com/2012/06/cascading-dropdown-knockoutjs-aspnet.html
It shows how to achieve cascading dropdown with MVC and knockout,js
Software Engineer by Profession, Learner by Passion!