我在 ActionResult 中传递两个列表变量,如下所示。
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
return View(book);
}
当我运行代码时出现以下错误
The model item passed into the dictionary is of type System.Collections.Generic.List1[Durgesh_Bhai.Models.category], but this dictionary requires a model item of type System.Collections.Generic.IEnumerable1[Durgesh_Bhai.Models.Books].
当我在 Actionresult 中只使用一个列表时,它工作正常。
最佳答案
我也是 mvc 的新手,但我得到的解决方案是创建一个类,它将所有你需要的对象作为数据成员保存,并传递该类的对象。
我创建了一个名为 data 的类,并将我所有的对象分配给此类的对象,并将该对象发送给模型。
或者你可以使用 View 包
Class Data
{
public List<category> cat {get;set;}
public List<Books> book {get;set;}
public Data()
{
this.cat = new List<category>();
this.book = new List<Books>();
}
}
public ActionResult Index()
{
Data d=new Data();
d.cat = _business.ViewAllcat().ToList();
d.book = _business.ViewAllBooks().ToList();
return View(d);
}
关于c# - 如何将多个参数传递给 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400571/