Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam fill DropDownListFor with funcion when submit button in form iam this error

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ID_post'.

C#
 public ActionResult AddUser()
        {
            if (ModelState.IsValid)
            {
                
                if (ViewData["Fill_Name_post"] == null && ViewData["Fill_Name_baker"] ==null)
                {
                ViewData["Fill_Name_post"] = ClassPost.GenerateNumbers();
                 ViewData["Fill_Name_baker"] = Classtypebaker.GenerateNumbers();
               }
                return View();
            }
            return View();
        }


[HttpPost]
       
        public ActionResult AddUser([Bind(Include = "FName, FFamily, FFamily,Fsenfi,Fholidays")]  User postData)
        {
            if (ModelState.IsValid)
            {
                 string status = "";
                User _User = new User();

                if (string.IsNullOrEmpty(postData.FName) || string.IsNullOrEmpty(postData.Fsenfi.ToString()) || string.IsNullOrEmpty(postData.FFamily) || string.IsNullOrEmpty(postData.Fholidays) || postData.FTypeBaker == 0 || postData.ID_post == 0)
                {
                    ViewBag.name = "test";
                      status = "0";
                }
                //todo: Add temp to repository
                if (postData == null)
                    return Json(false);
                //  int baker = _ClassPost.checkbaker(postData.);
                if (status == "1")
                {

                    db.Users.Add(postData);
                    db.SaveChanges();

                    
                }
                return View();
                //  else
                {

                  
                    // return Json(new { status }, JsonRequestBehavior.AllowGet);

                }
            }
            else
                return View();
        }

  public static List<SelectListItem> GenerateNumbers()
        {Context context = new Context();
            
            var numbers = (from p in context.Posts
                           select new SelectListItem
                           {
                               Text = p.FNamePost,
                               Value = p.ID_Post.ToString()
                           });
            return numbers.ToList();
        }
Posted

1 solution

1.You are using, in your list (method GenerateNumbers()), items of type SelectListItem , and this class does not have property named
ID_Post also neither property FNamePost.

2.The solution is to use the exiting properties of this class: Text and Value. For details see in MSDN[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900