0

I need to join two tables in my controller and pass the result to my view. Trouble is, I cannot seem to be able to access the column values from the joined table once I'm in the view.

I've tried to do a join in my controller, but I have a feeling I'm going in the wrong direction.

Controller:

//GET: kurser/stats/5
    public ActionResult Stats()
    {
        var stat = from k in db.kursister
                   join ku in db.kurser
                   on k.kursus_id equals ku.kursus_id
                   select k;

        return View(stat);
    }

My view:

@model IEnumerable<Itucation.Models.kursister>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div>
   @{ 
        foreach (var item in Model)
        {
            @Html.DisplayFor(modelItem => item.kursus_navn)<br/>
        }
    }
</div>

I would expect to be able to get the value of kursus_navn, which is in the kursus table, which is what I'm trying to join with the kursister table. But it doesn't work.

2
  • Looks like in your linq query, you are only selecting the k object. Currently you'll get all kursister records that have a matching kursus_id in kurser, but no data from the kurser table. Commented Apr 4, 2019 at 20:22
  • because you just select k and you can access only property of kursister model, so you can not access kurser because you have not select this property Commented Apr 5, 2019 at 4:40

1 Answer 1

1

You are only selecting the columns of db.kursister after joining the two tables. In order to select all columns from both db.kursus and db.kursister you need to change your select statement into this:

var stat = from k in db.kursister
                   join ku in db.kursus
                   on k.kursus_id equals ku.kursus_id
                   select new KursisterusViewModel {k = k, ku = ku};

Then in view:

<div>
   @{ 
        foreach (var item in Model)
        {
            @Html.DisplayFor(modelItem => item.ku.kursus_navn)<br/>
        }
    }
</div>

Update
Since anonymous type is not supported in a razor page, you need to create a new class like KursisterusViewModel to wrap your data into that:

public class KursisterusViewModel
{
    public kursister k { get; set; }
    public kursus ku { get; set; }
}

Then change the first line of the razor page into this:

@model IEnumerable<Itucation.ViewModels.KursisterusViewModel>

Assuming that KursisterusViewModel class is in ViewModels folder.
Note that Select statement was updated too.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much for answering. I've edited my code according to your answer, but now i get this error: 'kurser' does not contain a definition for 'ku' and no extension method 'ku' accepting a first argument of type 'kurser' could be found (are you missing a using directive or an assembly reference?)
@Lasserh The reason is because anonymous types are not supported in razor pages. I updated the answer. Let me know if you still have any issues.
Hi @rad, I'm making progress thanks to your help. However, I still get an error. this time it's The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType43[Itucation.Models.kursister,Itucation.Models.kursus_kursist,Itucation.Models.kurser]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Itucation.Models.KursisterusViewModel]' I've tried using return View(stat.ToList()); - but apparently that's not it. Do you have any more suggestions up your sleeve?
Did you apply all the edit that i made? for example did you modify the select statement to select new KursisterusViewModel {k = k, ku = ku}; ?
You're right, of course. After I read your updated answer properly and updated my code accordingly - everything works beautifully. thank you very, very much.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.