//ERROR
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProductID'.
- [HttpGet
- public bool SelectProduct(clproduct clproduct)
- {
- using (MySqlConnection con = new MySqlConnection())
- {
- con.ConnectionString = constr;
- using (MySqlCommand cmd = new MySqlCommand())
- {
- cmd.Connection = con;
- cmd.CommandText = "insert into clientproduct (ProductId,ClientProductDetail,Act_Date,Exp_Date,CLProductId,ParentProductId) values ('" + clproduct.ProductId + "','" + clproduct.ClientProductDetail + "',"
- + "'" + clproduct.Act_Date + "','" + clproduct.Exp_Date + "','" + clproduct.CLProductId + "','" + clproduct.ParentProductId + "')";
-
- con.Open();
- i = cmd.ExecuteNonQuery();
- con.Close();
-
- return true;
- }
- return false;
- }
- }
- ]
- public ActionResult SelectedProduct()
- {
- MySqlConnection con = new MySqlConnection(constr);
- MySqlDataAdapter da = new MySqlDataAdapter("SELECT ProductId,ProductName,Description FROM ProductMaster", con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- ViewBag.ProductNM = ToSelectList(dt, "ProductID", "ProductName");
- ViewBag.productid = ToSelectList(dt, "ProductId", "ProductId");
- ViewBag.desc = ToSelectList(dt, "ProductId", "Description");
- return View();
- }
- [HttpPost]
- public ActionResult SelectedProduct( clproduct cl)
- {
- string msg;
- if (ModelState.IsValid)
- {
- ModelState.Clear();
- try
- {
- if(i > 1)
- {
- SelectProduct(cl);
- Response.Write("<script> alert('Records added Successfully.')<script>");
- return RedirectToAction("Index");
- }
- else
- {
- msg = "All Fields Required";
- }
- }
-
- catch
- {
- Response.Write("<script> alert('Records not added..')<script>");
- return View("Index");
- }
- }
- else
- {
- return View("invalid");
- }
- ViewBag.Message = msg;
- return View();
- }
- [NonAction]
- public SelectList ToSelectList(DataTable dtable,string viewfield, string textfield)
- {
- List<SelectListItem> list = new List<SelectListItem>();
- foreach(DataRow drow in dtable.Rows)
- {
- list.Add(new SelectListItem()
- {
- Text =drow[textfield].ToString(),
- Value=drow[viewfield].ToString()
- });
- }
- return new SelectList(list, "Value", "Text");
- }
//MODEL
- public class clproduct
- {
- [Display(Name = "ClpID")]
-
- public string CLProductId { get; set; }
- [Display(Name = "Prod ID")]
- public string ProductId { get; set; }
-
- [Display(Name = "P_ProductID")]
- public string ParentProductId { get; set; }
-
- public string Act_Date { get; set; }
-
- public string Exp_Date { get; set; }
- [Display(Name = "Description")]
- public string ClientProductDetail { get; set; }
-
- }
View