セットアップはthis postと似ていますが、ラムダに基づいて別のモデルの項目リストから特定の項目を表示するためにhtmlヘルパーを使用しようとしています表現。異なるモデルを使用するビューからモデルリスト項目変数を表示
私の例では、アルバムとジャンルを使用します。
namespace MvcMusicStore.Models
{
public class Album
{
public int AlbumId { get; set; }
public int GenreId { get; set; }
public int ArtistId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string AlbumArtUrl { get; set; }
public Genre Genre { get; set; }
}
}
namespace MvcMusicStore.Models
{
public partial class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ThisAlbumId { get; set; }
public List<Album> Albums { get; set; }
}
}
ジャンルのモデルを使用し、私はそれは一つだけALBUMIDを返す必要がありAlbum.AlbumId == Genre.AlbumId
@foreach (var item in Model.Albums.Where(x=>x.AlbumId == Model.ThisAlbumId))
{
<td>@Html.DisplayFor(modelItem => item.Title)</td>
}
Album.Titleを表示しようとしてるのビューがあります。現時点では、「値はnullにはなりません」というエラーが発生しています。私はまた、
<td>@Html.DisplayFor(model => model.Album.Where(x => x.Id == model.ThisAlbumId).FirstOrDefault())</td>
を試みたが、私はエラーを取得:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions
すべてのヘルプや案内が大幅に高く評価されます。
あなたのラムダ式私はデバッグのさらに下に得ることができますmodelItem =>項目は意味をなさない、使用品=>アイテムの代わりに – Noppey