Search code examples
c#razorasp.net-mvc-5

ASP.NET MVC seems don't pass the associated values to its view


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


Solution

  • 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 IEnumerables. 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!