'Joining two tables in MVC .NET Core
I have two tables and I want to display them in my view with a SQL join. My join can be found in the controller:
public RechercheController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Zangen()
{
var werkzeuge = _context.Werkzeuge
.FromSqlRaw("SELECT * FROM dbo.Werkzeuge, dbo.Werkzeugmodelle
WHERE dbo.Werkzeuge.Typ=1
AND dbo.Werkzeuge.Werkzeugmodell=dbo.Werkzeugmodelle.Id")
.ToList();
ViewBag.Werkzeuge = werkzeuge;
return View();
}
The way I try to display them in my view:
<tbody>
@{
foreach (Werkzeug werkzeug in ViewBag.Werkzeuge)
{
<tr>
<td>@werkzeug.Id</td>
<td>@werkzeug.Number</td>
<td>@werkzeug.Owner</td>
<td>@werkzeug.Type</td>
@if (User.IsInRole("Admin"))
{
<td>
<form asp-action="CreateEdit" asp-route-id="@werkzeug.Id">
<button type="submit"
class="btn btn-warning">Bearbeiten</button>
</form>
</td>
}
</tr>
}
}
</tbody>
The for each loop only grabs the data from the table Werkzeuge but not those from the table Werkzeugmodelle.
- How can I display the data from table
Werkzeugmodelle?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
