C# – How to pass multiple parameters to a view

asp.net-mvccnet

I am passing two list variables in an ActionResult as Below.

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

When I run the Code I get the below error

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].

When I am using only one List in Actionresult its working fine.

Best Answer

I am also new to mvc but the solution i got was to create a class which will hold all you required objects as data members and pass the object of that class.

I created a class called data and assigned all my objects to the object of this class and sent that object to model.

or you can use view bag

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