Reputation: 677
I got a problem, Please help me on this Error : Additional information: There is no ViewData item of type 'IEnumerable' that has the key '
MyAction Code
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GiderEkle(Faturalar fatura, Giderler gideriki)
{
List<SelectListItem> item8 = new List<SelectListItem>();
foreach (var c in db.Caris)
{
item8.Add(new SelectListItem
{
Text = c.Adi,
Value = c.CariID.ToString()
});
}
ViewBag.countrydrop = item8;
return View();
}
My .cshtml Code
<div class="form-group">
<p>Müşterı Adı</p>
<div class="col-md-10">
@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.country,"Select country")
@Html.ValidationMessageFor(model => model.CariID, "", new { @class = "text-danger" })
</div>
</div>
My Model Cari,
public class Cari
{
[Key]
public int CariID { get; set; }
public IEnumerable<SelectListItem> DropDownItems { get; set; }
public bool MusteriTip { get; set; }
public string Unvan { get; set; }
public string TC { get; set; }
public string Adi { get; set; }
public string Soyadi { get; set; }
public string Adres { get; set; }
public string VD { get; set; }
public string VKN { get; set; }
public string Telefon { get; set; }
public string Email { get; set; }
public string WebSite { get; set; }
}
Faturalar model,
public class Faturalar
{
[Key]
public int FaturaID { get; set; }
public string Tarih { get; set; }
public int CariID { get; set; }
[ForeignKey("CariID")]
public Cari Caris { get; set; }
public int FaturaNo { get; set; }
public List<Giderler> Giderlers { get; set; }
}
Giderler model,
public class Giderler
{
[Key]
public int GiderlerID { get; set; }
public int FaturaID { get; set; }
[ForeignKey("FaturaID")]
public Faturalar Faturalar { get; set; }
public string Aciklama { get; set; }
public int Miktar { get; set; }
public string Birim { get; set; }
public double Fiyat { get; set; }
public double KDV { get; set; }
public double Tutar { get; set; }
}
and finally on the up side
@model UmtKontrolMerkezi.Models.Faturalar
Upvotes: 0
Views: 87
Reputation: 70814
The ViewBag property is called countrydrop
and you are attempting to cast ViewBag.country
to an IEnumerable<SelectListItem>
which does not exist.
You could use:
@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
Which would reference the correct variable you have defined within your GiderEkle Action Method
, however I would prefer a strongly typed view model over the ViewBag
approach.
I have proved this works by the following Fiddle:
https://dotnetfiddle.net/xXnQwy
Action Method:
public ActionResult DropDownListTest()
{
var myList = new List<SelectListItem>();
myList.Add(new SelectListItem { Text = "Darren one", Value = "1" });
myList.Add(new SelectListItem { Text = "Darren two", Value = "2" });
myList.Add(new SelectListItem { Text = "Darren three", Value = "3" });
ViewBag.DarrenList = myList;
return View();
}
CSHTML file:
@Html.DropDownList("DarrenId",(IEnumerable<SelectListItem>)ViewBag.DarrenList)
Upvotes: 2