Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I crated SelectList selectList with data from my Entities. And now I want send value from DropDownList to Entitie. everything is OK (I got value from selected item) , but I got his error et the end

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'Przelew.DaneKont'.
C#
@Html.DropDownList("Przelew.DaneKont", ViewBag.DaneKontList as SelectList, "-- Wybierz kontot --")



Viewmodel:
C#
@Html.DropDownList("Przelew.DaneKont", ViewBag.DaneKontList as SelectList, "-- Wybierz kontot --")


Controller:


C#
[Authorize]
        [HttpGet]
        public ActionResult WykonajPrzelew( ) {



            using (var context = new BankAppEntities1())
            {
                konto = context.Konto.SqlQuery("SELECT * FROM dbo.Konto WHERE UserId = '" + userIdValue + "' ").ToList();

            }


            WykonajPrzelewMultipleView mymodel = new WykonajPrzelewMultipleView();
            mymodel.Konta = konto;

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var item in konto){

                items.Add(new SelectListItem { Text = item.Nazwa+ "  Saldo :"+item.Saldo , Value = item.KontoId+"" });

             }

            ViewBag.DaneKont = items;

            SelectList selectList = new SelectList(items, "Value", "Text");
            ViewBag.DaneKontList = selectList;




            return View(mymodel);
        }


      [Authorize]
        [HttpPost]
        public ActionResult WykonajPrzelew(WykonajPrzelewMultipleView model)
        {



            string imie = model.Przelew.ImieOdbiorcy;
            string nazwisko = model.Przelew.NazwiskoOdbiorcy;
            int numerOdbiorcy = Convert.ToInt32(model.Przelew.NumerRachunkuOdbiorcy);
            int kontoID = Convert.ToInt32(model.Przelew.DaneKont); // I got Value from List 

            string tytul = model.Przelew.Tytuł;
            string ulica = model.Przelew.Ulica;
            string numerdomu = model.Przelew.nrdomu;
            string kod = model.Przelew.Kodpocztowy;
            string miasto = model.Przelew.Miasto;
       string tyt =model.Przelew.Tytuł;
            decimal kwota = model.Przelew.Kwota;
            DateTime date = System.DateTime.Now;



            using (var context2 = new BankAppEntities1())
            {
                var dane = new Przelew { Imie = imie, Nazwisko = nazwisko, NumerKontaOdbiorcy = numerOdbiorcy, KontoId = "1", NumerKontaNadawcy = 3444666, Ulica = ulica, Nr_domu = numerdomu,Kod_pocztowy="343", Miasto = miasto, Typ = "Normlany", UserId = userIdValue,Kwota=kwota , Date=date , Tytul=tyt };
                context2.Przelew.Add(dane);

                context2.SaveChanges();
            }

            return View();
        }
Posted
Comments
Mostafa Asaduzzaman 1-Jun-15 17:11pm    
I can see from your code (in WykonajPrzelew)
ViewBag.DaneKont = items;

SelectList selectList = new SelectList(items, "Value", "Text");
ViewBag.DaneKontList = selectList;
you calling in @Html.DropDownList("Przelew.DaneKont", ViewBag.DaneKontList as SelectList, "-- Wybierz kontot --")

try with @Html.DropDownList(ViewBag.DaneKont, ViewBag.DaneKontList as SelectList, "-- Wybierz kontot --")

1 solution

The problem is after posting data, you are returning the View but you are not populating ViewBag values. So when it executes DropDownListFor() helper in View, it throws exception as ViewBag.DaneKont is not set from action so it will be null, In your post action you need to add following code again before returning View, so that ViewBag values are available:

C#
    context2.SaveChanges();
  }

List<SelectListItem> items = new List<SelectListItem>();
 
    foreach (var item in konto)
   {
 
      items.Add(new SelectListItem 
                    { 
                      Text = item.Nazwa+ "  Saldo :"+item.Saldo , 
                      Value = item.KontoId+"" 
                    });
 
   }
 
   ViewBag.DaneKont = items;
 
   SelectList selectList = new SelectList(items, "Value", "Text");
   ViewBag.DaneKontList = selectList;

   return View();
}
 
Share this answer
 
v4

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