I have two tables: tblProduct and tblCategory.
I have a View with @model IEnumerable<xx.Models.tblProduct>, now I only get the data from tblProduct.
I want to have some columns from both tables in one View.
In the controller I have this:
public ActionResult Index(int id)
{
var q = from p in db.tblProducts
where p.CategoryId == id
select p;
Error:
Namespace.Models.bar is a 'type' but is used like a 'variable'
How can I fix this?
Well I fixed this by this:
var b = new bar();
return View(b);
But... now I get this error in the browser:
The model item passed into the dictionary is of type 'xx.Models.bar', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[xx.Models.bar]'.
Evil Tuinhek...
Member
6 Points
25 Posts
Retrieve two tables in one view
Nov 29, 2012 02:55 PM|LINK
Hi,
I have two tables: tblProduct and tblCategory.
I have a View with @model IEnumerable<xx.Models.tblProduct>, now I only get the data from tblProduct.
I want to have some columns from both tables in one View.
In the controller I have this:
public ActionResult Index(int id)
{
var q = from p in db.tblProducts
where p.CategoryId == id
select p;
return View(q);
}
How can I get those both tables in one view?
Thanks
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Retrieve two tables in one view
Nov 29, 2012 03:09 PM|LINK
make a ViewModel class from desired fields.
Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
RameshRajend...
Star
7983 Points
2099 Posts
Re: Retrieve two tables in one view
Nov 29, 2012 04:36 PM|LINK
http://stackoverflow.com/questions/10649412/2-table-views-in-one-view
ashoksudani
Participant
1043 Points
213 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 06:20 AM|LINK
for this you need to create Model and pass This model to View like
Public class Data
{
public tblProducts Product{get;set}
public TblCategory Category{get;set;}
}
Evil Tuinhek...
Member
6 Points
25 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 07:51 AM|LINK
How do I pass the model to the View?
in public ActionResult Index(){
}
Can you give me a sample code?
RameshRajend...
Star
7983 Points
2099 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 08:01 AM|LINK
Hai
public ActionResult Index(){
Return(YourModelName)
}
And right clcik the methode inside ,select "go to view".
Gud luck..
Evil Tuinhek...
Member
6 Points
25 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 08:10 AM|LINK
Hi,
I called my class bar to test.
I get an error in
return View(bar);
Error:
Namespace.Models.bar is a 'type' but is used like a 'variable'
How can I fix this?
Evil Tuinhek...
Member
6 Points
25 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 08:13 AM|LINK
Well I fixed this by this:
var b = new bar();
return View(b);
But... now I get this error in the browser:
The model item passed into the dictionary is of type 'xx.Models.bar', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[xx.Models.bar]'.
RameshRajend...
Star
7983 Points
2099 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 08:21 AM|LINK
Hai
Change
bar b = new bar();
return View(b);
and
add the namesspace
Evil Tuinhek...
Member
6 Points
25 Posts
Re: Retrieve two tables in one view
Nov 30, 2012 08:25 AM|LINK
Hi,
Done that but don't getting something out of the database :(
This is how my class bar looks like:
namespace xx.Models {
public class bar {
public tblProduct product { get; set; }
public tblCategory category { get; set; }
}
}
And in the View: @xx.Models.bar
and than:
@Html.DisplayFor(model => model.category.Name)
But I get nothing
Is my model class right?