我有一個類似於this post的設置,並試圖使用html助手在基於lambda的單獨模型中的項目列表中顯示特定項目表達。從使用不同模型的視圖中顯示模型列表項變量
我會在我的例子中使用專輯和流派。
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; }
}
}
有使用該類型模型的圖,我試圖顯示Album.Title其中Album.AlbumId == Genre.AlbumId
@foreach (var item in Model.Albums.Where(x=>x.AlbumId == Model.ThisAlbumId))
{
<td>@Html.DisplayFor(modelItem => item.Title)</td>
}
應該只返回一個ALBUMID。目前我收到「Value can not be 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
任何幫助或指導是極大的讚賞。
你的lambda表達式modelItem =>項是沒有意義的,使用項目=>項目,而不是 – Noppey