Question

Update:

enter image description here After using Include, the name still doesn't show up. Then I did the debugging and found the odd stuff.

I inspect the users inside the db object and found its count=0(see 1), then I clicked a refresh icon which located in area 3 so that I can see the Results View in area 2. After doing that, the users are loaded. But I still cannot get users at the first place even I specified

 db.Configuration.LazyLoadingEnabled = false;

. Please help


Post Start from here:

I am learning MVC and I have a very simple data model.

Say meeting relates its organizer which is an instance of a user.

And I am using EF6.

Here is my code

public ActionResult Index()
        {
            myEntities db = new myEntities();

            return View(db.meetings);
        }

Each meeting entity associates with a group users and it has a field called organizer_id which is a user id.

I'd like to list all meetings on my home page and their organizers' names.

Here is my Razor code

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.event_date_time)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.users.SingleOrDefault(u => u.id == item.organizer_id).name)
        </td>
        ..............

In the organizer name cell, it will always display nothing. I did a step by step debugging and found that I have access to the associated users for each meeting in back-end C# code, but when it goes in the Razor, the item only has its own values, but the associated users which are item.users are null(because I checked item.users.Count was 0).

So that's why I came to the conclusion.

Could anyone point out my mistake please?

Thanks in advance

Was it helpful?

Solution 2

I finally solved this issue by myself.

here is what I did(To be honest, I know this is not the "standard" way to do it, if you know how to fix this issue properly and decently, please DO ADD YOUR ANSWERS. THANK YOU!)

    var events = db.meetings.ToList();
    var ers = db.users.ToList();

    List<MeetingModel> models = events.Select(i =>
        new MeetingModel { 
            Title = i.title,
            Address = i.address,
            Date = i.event_date_time,
            Description = i.description,
            OrganizerName = ers.FirstOrDefault(u => u.id == i.organizer_id).name
        }).ToList();

I force both entities to become lists which are not deferred IEnumberables. In this way, I know when the program is running to these statements, the values of both two have to be stored in these 2 variables.

But this is definitely not the proper way to do it. Anyone who knows how to solve it, PLEASE DO ADD YOUR ANSWERS.

THANK YOU VERY MUCH!

OTHER TIPS

This is because your Context is out of scope on your view. What you need to do is include all of the users in your controller so that in your view it is searching in memory.

public ActionResult Index()
{
    myEntities db = new myEntities();
    var model = db.meetings.Include("users").ToList();
    return View(model);
}

A better solution is to create a specific model for your page in which all necessary data is retrieved and put into a custom model for your view:

public class MeetingModel
{
    public string Title{get;set;}
    public string Address {get;set;}
    public DateTime Date {get;set;}
    public string Description {get;set;}
    public string OrganizerName {get;set;}
}

In your controller:

public ActionResult Index()
{
    myEntities db = new myEntities();
    List<MeetingModel> models = db.meetings.Select(i =>
        new MeetingModel {
            Title = i.title,
            Address = i.address,
            Date = i.event_date_time,
            Description = i.description,
            OrganizerName = i.users.SingleOrDefault(u => u.id == i.organizer_id).name
        }).ToList();
    return View(models);
}

Then, change your view to take an model of List:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OrganizerName)
        </td>
        ......
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top