【问题标题】:How to pass multiple parameters to a view?如何将多个参数传递给视图?
【发布时间】:2015-10-02 17:35:20
【问题描述】:

我在 ActionResult 中传递了两个列表变量,如下所示。

public ActionResult Index()
{
    List<category> cat = _business.ViewAllcat().ToList();
    List<Books> book = _business.ViewAllBooks().ToList();
    return View(book);
}

当我运行代码时,出现以下错误

传入字典的模型项是类型 System.Collections.Generic.List1[Durgesh_Bhai.Models.category],但是 这个字典需要一个类型的模型项 System.Collections.Generic.IEnumerable1[Durgesh_Bhai.Models.Books]。

当我在 Actionresult 中只使用一个 List 时,它工作正常。

【问题讨论】:

标签: c# .net asp.net-mvc


【解决方案1】:

我也是 mvc 的新手,但我得到的解决方案是创建一个类,它将所有需要的对象作为数据成员保存并传递该类的对象。

我创建了一个名为 data 的类并将我的所有对象分配给该类的对象并将该对象发送到模型。

或者你可以使用查看包

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

【讨论】:

    【解决方案2】:

    请创建一个新的 ViewModel 类并像这样存储您的两个列表:

    public class MyViewModel
    {
        public List<Category> Categories { get; set; }
        public List<Book> Books { get; set; }
    
        public MyViewModel()
        {
            this.Categories = new List<Category>();
            this.Books = new List<Book>();
        }
    }
    
    public ActionResult Index()
    {
        MyViewModel model = new MyViewModel();
        model.Categories = _business.ViewAllcat().ToList();
        model.Books = _business.ViewAllBooks().ToList();
        return View(model);
    }
    

    然后在您的视图 (index.cshtml) 中,像这样声明 MyViewModel:

    @model WebApp.Models.MyViewModel
    
    <div>
        your html
    </div>
    

    我们刚刚使用的概念称为视图模型。请在此处阅读更多信息: Understanding ViewModel

    【讨论】:

      【解决方案3】:

      您应该创建一个 ViewModel 类(只是一个 .cs 类),其中包含您在页面上需要的所有内容。

      然后是视图的第一行,你应该使用viewmodel类作为你的模型。

      然后在控制器操作中填充您的模型,例如:

      public ActionResult Index()
          {
              List<category> cat = _business.ViewAllcat().ToList();
              List<Books> book = _business.ViewAllBooks().ToList();
              return View(new MyViewModel() { Cats = cat, Books = book);
          }
      

      然后您就可以访问页面上的所有内容了。

      【讨论】:

        【解决方案4】:

        创建一个包含两个列表的新对象。

        public ActionResult Index()
        {
            List<category> cat = _business.ViewAllcat().ToList();
            List<Books> book = _business.ViewAllBooks().ToList();
            return View(new CatBooks { Cats = cat, Books = book });
        }
        
        public class CatBooks {
            public List<category> Cats { get; set; }
            public List<Books> Books { get; set; }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-01-13
          • 2011-12-09
          • 1970-01-01
          • 2011-06-27
          • 1970-01-01
          • 2011-08-10
          • 1970-01-01
          • 2022-11-23
          • 1970-01-01
          相关资源
          最近更新 更多