gpt4 book ai didi

c# - 附加信息: There is no ViewData item of type 'IEnumerable Asp.NET MVC

转载 作者:行者123 更新时间:2023-12-03 08:21:15 26 4
gpt4 key购买 nike

我遇到了问题
请帮我
错误:其他信息:没有类型为“IEnumerable”的ViewData项,其键为“

MyAction代码

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

我的.cshtml代码
<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>

我的模特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模型,
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; }
}

吉德勒模型
 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; }
}

最后是好的一面
@model UmtKontrolMerkezi.Models.Faturalar

最佳答案

ViewBag属性称为countrydrop,您正尝试将ViewBag.country转换为不存在的IEnumerable<SelectListItem>

您可以使用:

@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")

这将引用您在GiderEkle Action Method中定义的正确变量,但是与 ViewBag方法相比,我更喜欢强类型 View 模型。

我已经通过以下 fiddle 证明了这一工作:

https://dotnetfiddle.net/xXnQwy

操作方法:
    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文件:
@Html.DropDownList("DarrenId",(IEnumerable<SelectListItem>)ViewBag.DarrenList)

关于c# - 附加信息: There is no ViewData item of type 'IEnumerable<SelectListItem> Asp.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com