本文介绍了从Html.DropDownList获取Value后出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的实体中的数据创建了SelectList selectList。现在我想从DropDownList向Entitie发送值。一切都很好(我从选定的项目中得到了价值),但我得到了他的错误和结束



发生了'System.InvalidOperationException'类型的异常System.Web.Mvc.dll但未在用户代码中处理附加信息:没有类型为'IEnumerable'的ViewData项具有键'Przelew.DaneKont'

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'.

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







Viewmodel:




Viewmodel:

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





控制器:







Controller:


[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();
        }

推荐答案

    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();
}


这篇关于从Html.DropDownList获取Value后出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:02