Hi,
I am currently learning MVC 3.0 and have performed basic tasks insert, update etc on on table. Now I was to display my data from multiple tables ( Order and OrderDetail) in one view and also want to insert data in two tables from one view. What should my controller
should pass to the view? I am using Ado.net Entity Data Model. I have gone on internet but for my disappointment no solution could appear to be suggesting any way out for this :( Please guide me how can I do this?
public ActionResult Index()
{
OrderViewData orderView = new OrderViewData();
orderView.OrderData = from o in db.Orders select o;
orderView.OrderDetailData = from or in db.OrderDetails select or;
return View(orderView);
}
Now I am passing this in my View as
@model IEnumerable<MvcApplication.Models.OrderViewData>
Now in foreach I get two table which I had made properties of the class OrderViewData.. Now how i display the specific columns from both tables on the view?
The model item passed into the dictionary is of type 'MvcApplication.Models.OrderViewData', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication.Models.OrderViewData]'.
Please guide me considering me basic learning student !
YOu strongly typed your view with a model of type list. However you pass a single class that contains two lists. Thus please correct the @model directive of your view. Moreover, once you get data with linq, before putting them in a VieModel transform them
into lists:
(....linq statemnent.....).ToList()
otherwise they are IQueryables, and IQueryables may cause problems when put in a View.
About the two data....simple put them into two different html tables :) if this make sense in your application.
If you want to align them someway, displaying them on the same htmlTable you have two merge couples of objects in the two lists into single objects containing properties form both objects
I have made changes as you said..But my view is giving the same exception. Please tell what is problem with my view regarding the " correct @model directive"??
//My ViewModel class
public class OrderViewData
{
//
public List<Order> OrderData { get; set; }
public List<OrderDetail> OrderDetailData { get; set; }
}
//My Index() code
public ActionResult Index()
{
OrderViewData orderView = new OrderViewData();
orderView.OrderData = (from o in db.Orders select o).ToList();
orderView.OrderDetailData = (from or in db.OrderDetails select or).ToList();
return View(orderView);
}
// here is my @model directive
@model List<MvcApplication.Models.OrderViewData>
:-(
I
tryall the time to display twocolumnsfrom two
tablesbutsomehow I
don't find the solution i found your thread and I think itwill help mea bit socanyouplease postme your
correct code?
touseef_84
Member
9 Points
5 Posts
How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 06:05 AM|LINK
Hi,
I am currently learning MVC 3.0 and have performed basic tasks insert, update etc on on table. Now I was to display my data from multiple tables ( Order and OrderDetail) in one view and also want to insert data in two tables from one view. What should my controller should pass to the view? I am using Ado.net Entity Data Model. I have gone on internet but for my disappointment no solution could appear to be suggesting any way out for this :( Please guide me how can I do this?
francesco ab...
All-Star
20910 Points
3278 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 06:39 AM|LINK
Simple You define a View Model and put the object containing each the data of a table in a different property of the ViewModel
Mvc Controls Toolkit | Data Moving Plug-in Videos
touseef_84
Member
9 Points
5 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 07:28 AM|LINK
@francesco
I am using this code in my controller
public ActionResult Index() { OrderViewData orderView = new OrderViewData(); orderView.OrderData = from o in db.Orders select o; orderView.OrderDetailData = from or in db.OrderDetails select or; return View(orderView); } Now I am passing this in my View as @model IEnumerable<MvcApplication.Models.OrderViewData> Now in foreach I get two table which I had made properties of the class OrderViewData.. Now how i display the specific columns from both tables on the view?touseef_84
Member
9 Points
5 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 07:36 AM|LINK
Here I am posting the script i am using on view and the exception it throws
@model IEnumerable<MvcApplication.Models.OrderViewData> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> OrderDesc </th> <th> Amount </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.OrderData) </td> <td> @Html.DisplayFor(modelItem => item.OrderDetailData) </td> @* <td> @Html.ActionLink("Edit", "Edit", new { id=item.OrderId }) | @Html.ActionLink("Details", "Details", new { id=item.OrderId }) | @Html.ActionLink("Delete", "Delete", new { id=item.OrderId }) </td>*@ </tr> } </table> And it throws this exceptionThe model item passed into the dictionary is of type 'MvcApplication.Models.OrderViewData', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication.Models.OrderViewData]'.
Please guide me considering me basic learning student !
francesco ab...
All-Star
20910 Points
3278 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 08:31 AM|LINK
YOu strongly typed your view with a model of type list. However you pass a single class that contains two lists. Thus please correct the @model directive of your view. Moreover, once you get data with linq, before putting them in a VieModel transform them into lists:
(....linq statemnent.....).ToList()
otherwise they are IQueryables, and IQueryables may cause problems when put in a View.
About the two data....simple put them into two different html tables :) if this make sense in your application.
If you want to align them someway, displaying them on the same htmlTable you have two merge couples of objects in the two lists into single objects containing properties form both objects
Mvc Controls Toolkit | Data Moving Plug-in Videos
touseef_84
Member
9 Points
5 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 09:00 AM|LINK
I have made changes as you said..But my view is giving the same exception. Please tell what is problem with my view regarding the " correct @model directive"??
//My ViewModel class public class OrderViewData { // public List<Order> OrderData { get; set; } public List<OrderDetail> OrderDetailData { get; set; } } //My Index() code public ActionResult Index() { OrderViewData orderView = new OrderViewData(); orderView.OrderData = (from o in db.Orders select o).ToList(); orderView.OrderDetailData = (from or in db.OrderDetails select or).ToList(); return View(orderView); } // here is my @model directive @model List<MvcApplication.Models.OrderViewData> :-(touseef_84
Member
9 Points
5 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Jul 15, 2011 09:10 AM|LINK
Thanks sir,
I read your last reply again and again i figure out the problem in my code.
it is resolved now
thankssssssssssssssssssssss u sirrrrrrrrrrr
elecmen
Member
9 Points
9 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Sep 20, 2011 03:18 PM|LINK
@Toussef
I try all the time to display two columns from two tables but somehow I don't find the solution
i found your thread and I think it will help me a bit
so can you please post me your correct code?
thank you
sajan064
Member
2 Points
1 Post
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Feb 21, 2012 07:53 AM|LINK
@touseef_84
how u sove d problem ?
plz help me m get stuck wit same problem
Evil Tuinhek...
Member
6 Points
25 Posts
Re: How to Display Data in a single view from Multiple Tables in MVC 3.0?
Nov 30, 2012 12:46 PM|LINK
Can you please tell us the steps you made to get it work?
Thanks