从Html.DropDownList获取Value后出现奇怪的错误 [英] Strange error after getting Value from Html.DropDownList

查看:80
本文介绍了从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();
        }

推荐答案

问题是在发布数据后,您将返回视图,但是您没有填充 ViewBag 值。因此,当它在View中执行 DropDownListFor()帮助时,它会抛出异常,因为 ViewBag.DaneKont 未从动作设置,因此它将为null,在你的帖子操作中你需要在返回View之前再次添加以下代码,以便 ViewBag 值可用:



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:

    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后出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆