c# - 如何将多个参数传递给 View ?

标签 c# .net asp.net-mvc

我在 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/

相关文章:

c# - 如何在不是有效 xml 文件的文件中生成关闭节点?

c# - 为什么 (object)0 == (object)0 不同于 ((object)0).Equals((object)0)?

.net - 在多行文本框中格式化字符串

css - 在 Razor Grid 输出中使用 css

asp.net-mvc - Azure ASP.Net Core 网站中的 301 重定向

c# - 将 Kinect 应用程序连接到 Kinect Studio

c# - "illegal project name"monoDevelop - Ubuntu?

c# - 如何在 .NET 图表的 Y 轴上对数据进行分组?

.net - 如何构建附加到 .NET 进程并监听特定类型事件的自定义调试器?

c# - 生成过滤条件更新不起作用