DavidN
DavidN

Reputation: 111

How to list the records from different tables inside cshtml view?

I am having a problem in a ASP.NET MVC project. I have a database where I have several tables, and I want to list all the records inside each table in my View, I am able to do this using only one table, I am going to provide here the code that works with one table and what I am trying to do.

Controller:

Using only one table I would do:

Mp5DataclassesDataContext db = new Mp5DataclassesDataContext();
public ActionResult Admin()
{
    return View(db);
}

This is what I am trying to do:

    return View(db);

At this point I can debug and see that all my tables are in that db object, with all the correct data. Then the problem is in my View

View:

@*This is defined at the top of my .cshtml*@
@model IEnumerable<Interface_AutoUtad.Models.Mp5DataclassesDataContext>

@*This is the code that works with only one table*@
@foreach (var item in Model)
{
     @Html.DisplayFor(modelItem => item.table_column)
}

@*What I am trying to do:*@
@foreach (var item in Model)
{
     @Html.DisplayFor(modelItem => item.some_other_table.table_column)
}

I can't get this last part to work because "some_other_table" doesn't show up. Is there a way I can achieve this ? I want to Iterate through each table and inside iterate through each record in that table.

Thank you all in advance :)

Upvotes: 0

Views: 1459

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

It looks like you've set the "model" for your view to be your application's DbContext. Don't do that. Views are only processed at runtime, and querying into your database is way too much logic for a view. Any errors will only be exposed at runtime, and you have a huge potential for errors.

Views are designed to work with only one type. If you need to work with multiple types in the same view, you can either utilize a view model or use child actions.

View Model

public class MultipleTypesViewModel()
{
    public IEnumerable<SomeType> SomeTypes { get; set; }
    public IEnumerable<OtherType> OtherTypes { get; set; }
    ...
}

Then in your view:

@model Namespace.To.MultipleTypesViewModel

...

@foreach (var item in Model.SomeTypes)
{
    @Html.DisplayFor(modelItem => item.table_column)
}

@foreach (var item in Model.OtherTypes)
{
    @Html.DisplayFor(modelItem => item.table_column)
}

...

Child Actions

[ChildActionOnly]
public ActionResult ListSomeTypes()
{
    var someTypes = db.SomeTypes.ToList();
    return PartialView(someTypes);
}

[ChildActionOnly]
public ActionResult ListOtherTypes()
{
    var otherTypes = db.OtherTypes.ToList();
    return PartialView(otherTypes);
}

...

Then, create an appropriate partial view for each:

ListSomeTypes.cshtml

@model IEnumerable<Namespace.To.SomeType>

@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.table_column)
}

ListOtherTypes.cshtml

@model IEnumerable<Namespace.To.OtherType>

@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.table_column)
}

Etc. And then finally in your main view:

@Html.Action("ListSomeTypes")
@Html.Action("ListOtherTypes")

(In this case, the model of the main view is totally irrelevant.)

Upvotes: 1

Related Questions