In meiner ASP.NET MVC Core-App übergebe ich von einer unten gezeigten Aktionsmethode die Daten von Blogs und die zugehörigen Daten aus der Posts-Tabelle an eine Ansicht als return View(await _context.Blogs.Include(p => p.Posts).ToListAsync());
Seit ich bin Wenn ich Daten von zwei Tabellen übergebe, brauche ich ein ViewModel wie unten gezeigt. Frage: Wie kann ich ViewModel verwenden, um die zugehörigen Daten von meiner Controller Action-Methode zur Ansicht weiterzuleiten?Entity Framework Eager Loading - Daten an ViewModel übergeben
Im folgenden Code erhalte ich die offensichtlichen Fehler:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List'1[ASP_Core_Blogs.Models.Blog]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IList'1[ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel]'.
Modell:
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int PostYear { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
-Controller:
[HttpGet]
public async Task<IActionResult> Test(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View(await _context.Blogs.Include(p => p.Posts).ToListAsync());
}
Ansichtsmodell:
public class BlogsWithRelatedPostsViewModel
{
public int BlogID { get; set; }
public int PostID { get; set; }
public string Url { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int PostYear { get; set; }
}
Ansicht:
@model IList<ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel>
<div class="row">
<div class="col-md-12">
<form asp-controller="DbRelated" asp-action="EnterGrantNumbers" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th>Url</th>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
@for (int t = 0; t < Model.Count; t++)
{
<tr>
<td><input type="hidden" asp-for="@Model[t].BlogID" /></td>
<td><input type="hidden" asp-for="@Model[t].PostID" /></td>
<td>
<input type="text" asp-for="@Model[t].Url" style="border:0;" readonly /> <!--Not using /*Html.DisplayFor(modelItem => Model[t].Url)*/ since it does not submit stateName on Post. Not using <label asp-for=.....> since Bootstrap bold the text of <label> tag-->
</td>
<td>
<input asp-for="@Model[t].Title" />
</td>
<td>
<input asp-for="@Model[t].Content" />
</td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
Ich erhalte den Fehler auf 'ToLisAsync()' wie: 'IEnumerable 'enthält keine Definition für 'ToListAsync' und keine Erweiterungsmethode 'ToListAsync' ein erstes Argument vom Typ akzeptieren' IEnumerable 'könnte gefunden werden' –
nam
mit 'ToList()' Ich erhalte den Fehler: 'InvalidOperationException: Das in das ViewDataDictionary übergebene Modellelement ist vom Typ 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1 [ASP_Core_Blogs. Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel] ', aber diese ViewDataDictionary-Instanz benötigt ein Modellelement vom Typ' System.Collections.Generic.List'1 [ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel] '. '.In der @ Model-Direktive der Ansicht habe ich 'IList <...>' in 'List <...>' geändert, bevor dieser Fehler auftritt. – nam
Das macht nicht viel Sinn, es ist so, als ob Sie das Ergebnis Ihrer Abfrage übergeben hätten, ohne jedoch ToList aufzurufen. Sind Sie sicher, dass diese Aktion? – octavioccl