Click here to Skip to main content
14,937,945 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi

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

XML
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.List`1[Durgesh_Bhai.Models.category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Durgesh_Bhai.Models.Books]'.


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

Please provide the solution for the same as this is a urgent requirement.
Posted
Comments
F-ES Sitecore 14-Jul-15 3:55am
   
I don't see how the code you have posted will give that error. I can see "return View(cat)" giving that error, but not your code.

Also if this urgent then google "asp.net mvc two models view" and you'll find thousands of answers to your question instantly without the need to wait. Please do basic research before asking a question.

Hello,

I Suggest to use TupleList for Return more than one List and access it.

Just do Following...
C#
public ActionResult Index()
        {
            List&lt;category&gt; cat = _business.ViewAllcat().ToList();
            List&lt;Books&gt; book = _business.ViewAllBooks().ToList();

var objTuple=new Tuple<List<category>,List<Books>>(cat,book);
            return View(objTuple);
        }

// Access TupleList at Index.cshtml 
@model  Tuple<List<cat>,List<book>>
   
v4
Better you use ViewModel in this case , Create a viewmodel with require entities in your View and set values on controller and pass ViewModel to View.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900