Guest User

Guest User

  • Tech Writer
  • 189
  • 24.8k

i cant solve this error please help me

Aug 1 2018 1:52 AM
//ERROR
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProductID'.
  1. [HttpGet  
  2. public bool SelectProduct(clproduct clproduct)    
  3. {    
  4.     using (MySqlConnection con = new MySqlConnection())    
  5.     {    
  6.         con.ConnectionString = constr;    
  7.         using (MySqlCommand cmd = new MySqlCommand())    
  8.         {    
  9.            cmd.Connection = con;    
  10.            cmd.CommandText = "insert into clientproduct (ProductId,ClientProductDetail,Act_Date,Exp_Date,CLProductId,ParentProductId) values ('" + clproduct.ProductId + "','" + clproduct.ClientProductDetail + "',"    
  11.                        + "'" + clproduct.Act_Date + "','" + clproduct.Exp_Date + "','" + clproduct.CLProductId + "','" + clproduct.ParentProductId + "')";    
  12.                       
  13.            con.Open();    
  14.            i = cmd.ExecuteNonQuery();                        
  15.            con.Close();    
  16.                        
  17.            return true;    
  18.         }    
  19.         return false;    
  20.     }    
  21. }    
  22. ]    
  23. public ActionResult SelectedProduct()    
  24. {    
  25.     MySqlConnection con = new MySqlConnection(constr);    
  26.     MySqlDataAdapter da = new MySqlDataAdapter("SELECT ProductId,ProductName,Description FROM ProductMaster", con);    
  27.     DataTable dt = new DataTable();    
  28.     da.Fill(dt);    
  29.     ViewBag.ProductNM = ToSelectList(dt, "ProductID""ProductName");    
  30.     ViewBag.productid = ToSelectList(dt, "ProductId""ProductId");    
  31.     ViewBag.desc = ToSelectList(dt, "ProductId""Description");    
  32.     return View();    
  33. }    
  34. [HttpPost]    
  35. public ActionResult SelectedProduct( clproduct cl)    
  36. {    
  37.     string msg;    
  38.     if (ModelState.IsValid)    
  39.     {    
  40.          ModelState.Clear();    
  41.          try    
  42.          {    
  43.             if(i > 1)    
  44.             {    
  45.                 SelectProduct(cl);    
  46.                 Response.Write("<script> alert('Records added Successfully.')<script>");    
  47.                 return RedirectToAction("Index");    
  48.             }    
  49.             else    
  50.             {    
  51.                 msg = "All Fields Required";    
  52.             }    
  53.          }    
  54.     
  55.          catch    
  56.          {    
  57.              Response.Write("<script> alert('Records not added..')<script>");    
  58.              return View("Index");    
  59.          }    
  60.     }    
  61.     else    
  62.     {    
  63.        return View("invalid");    
  64.     }    
  65.     ViewBag.Message = msg;    
  66.     return View();    
  67. }    
  68. [NonAction]    
  69. public SelectList ToSelectList(DataTable dtable,string viewfield, string textfield)    
  70. {    
  71.     List<SelectListItem> list = new List<SelectListItem>();    
  72.     foreach(DataRow drow in dtable.Rows)    
  73.     {    
  74.         list.Add(new SelectListItem()    
  75.         {    
  76.             Text =drow[textfield].ToString(),    
  77.             Value=drow[viewfield].ToString()    
  78.         });    
  79.     }    
  80.     return new SelectList(list, "Value""Text");     
  81. }  
//MODEL
  1. public class clproduct    
  2. {    
  3.     [Display(Name = "ClpID")]    
  4.         
  5.     public string CLProductId { getset; }    
  6.     [Display(Name = "Prod ID")]    
  7.     public string ProductId { getset; }    
  8.      
  9.     [Display(Name = "P_ProductID")]    
  10.     public string ParentProductId { getset; }    
  11.     
  12.     public string Act_Date { getset; }    
  13.         
  14.     public string Exp_Date { getset; }    
  15.     [Display(Name = "Description")]    
  16.     public string ClientProductDetail { getset; }    
  17.      
  18. }  
View
  1. @model Clients.Models.clproduct    
  2. @{    
  3.     Layout = null;    
  4. }    
  5.     
  6. <!DOCTYPE html>    
  7.     
  8. <html>    
  9. <head>    
  10.     <meta name="viewport" content="width=device-width" />    
  11.     <title>Select Product</title>    
  12. </head>    
  13. <body>    
  14.     <div>    
  15.         @using (Html.BeginForm("SelectedProduct""ClientMaster", FormMethod.Post, new { id = "popupForm" }))    
  16.         {    
  17.             if (ViewBag.Message != null)    
  18.             {    
  19.                 <div class="alert alert-danger">    
  20.                     @ViewBag.Message    
  21.                 </div>    
  22.             }    
  23.             <div class="mycontainer">    
  24.                 <div class="row">    
  25.                     <div class="col-lg-10">    
  26.                         <table class="table table-responsive">    
  27.                             <tr>    
  28.                                 <td colspan="3">    
  29.                                     <div class="col-md-6"><label>ClPID</label></div>    
  30.                                     <div class="col-md-8">@Html.TextBoxFor(model => model.CLProductId)</div>    
  31.                                 </td>    
  32.                             </tr>    
  33.                             <tr>    
  34.                                 <td>    
  35.                                     <div class="col-md-6"><label>ProductID</label></div>    
  36.                                     <div class="col-md-8">    
  37.                                         @*<select id="ProductID" class="pc form-control" >    
  38.                                             <option>@ViewData["ProdID"]</option>    
  39.                                         </select>*@    
  40.                                         @Html.DropDownList("ProductID", (IEnumerable<SelectListItem>)ViewBag.ProductNM, "Select Product"new { @class = " form-control", @style = "width:200px" })    
  41.                                     </div>    
  42.                                 </td>    
  43.                                 <td>    
  44.                                     <div class="col-md-6"><label>Product</label></div>    
  45.                                     <div class="col-md-8">@Html.TextBoxFor(model => model.ClientProductDetail)</div>    
  46.                                 </td>    
  47.                                 <td>    
  48.                                     <div class="col-md-6"><label>Description</label></div>    
  49.                                     <div class="col-md-8"><input type="text" id="txt_Description" /></div>    
  50.                                 </td>    
  51.                             </tr>    
  52.                             <tr>    
  53.                                 <td>    
  54.                                     <div class="col-md-6"><label>Act_Date</label></div>    
  55.                                     <div class="col-md-8">@Html.TextBoxFor(model => model.Act_Date, new { @id ="ADate"})</div>    
  56.                                 </td>    
  57.                                 <td>    
  58.                                     <div class="col-md-6"><label>Exp_Date</label></div>    
  59.                                     <div class="col-md-8">@Html.TextBoxFor(model => model.Exp_Date, new { @id = "EDate" })</div>    
  60.                                 </td>    
  61.                                 <td>    
  62.                                     <div class="col-md-6"><label>Parent ProdID</label></div>    
  63.                                     <div class="col-md-8">@Html.TextBoxFor(model => model.ParentProductId)</div>    
  64.                                 </td>    
  65.                             </tr>    
  66.                         </table>    
  67.                     </div>    
  68.                 </div>    
  69.                 <div class="col-lg-10">    
  70.                     <table class="table table-responsive">    
  71.                         <tr>    
  72.                             <td>    
  73.                                 <div class="col-md-8">    
  74.                                     <input type="submit" class="btn btn-success" />    
  75.                                 </div>    
  76.                             </td>    
  77.                             <td>    
  78.                                 <div class="col-md-8">    
  79.                                     <input type="reset" class="btn btn-default" />    
  80.                                 </div>    
  81.                             </td>    
  82.                         </tr>    
  83.                     </table>    
  84.                 </div>    
  85.             </div>    
  86.         }    
  87.     </div>    
  88. </body>    
  89. </html>    
  90. @*<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>    
  91. <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>*@    
  92. @* *@    
  93. <script type="text/javascript">    
  94.     $(function () {    
  95.         $('#ADate').datepicker({    
  96.             dateFormat: 'mm-dd-yy'    
  97.         })    
  98.         $('#EDate').datepicker({    
  99.             dateFormat: 'mm-dd-yy'    
  100.         })    
  101.     })    
  102. </script>

Answers (8)