[英]View with partial view that also has IEnumerable
I have an application for college football fightsongs.我有一个大学橄榄球斗歌的申请。 I want to display a single teams's details like school name, mascot, conference, etc... but also have a partial view on the same page where the partial view loops through the teams's fight songs.我想显示单个球队的详细信息,如学校名称、吉祥物、会议等……但在同一页面上也有局部视图,局部视图循环播放球队的战斗歌曲。 I was able to achieve this with using an IEnumerable for the fight songs and Viewbag for the data, but I don't believe that is best practices.我能够通过将 IEnumerable 用于战斗歌曲并使用 Viewbag 作为数据来实现这一点,但我认为这不是最佳实践。
I want my code to be as lean as possible and having 30 Viewbags in my view is probably not the best way to do it.我希望我的代码尽可能精简,在我看来拥有 30 个 Viewbags 可能不是最好的方法。
My code blows up on @Html.Partial("_FightSongDetails")
我的代码在@Html.Partial("_FightSongDetails")
Here is the error:这是错误:
Additional information: The model item passed into the dictionary is of type 'SportSongs.Models.ViewModels.ConferenceTeamViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SportSongs.Models.ViewModels.ConferenceTeamViewModel]'.附加信息:传递到字典中的模型项的类型为“SportSongs.Models.ViewModels.ConferenceTeamViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[SportSongs.Models.ViewModels.ConferenceTeamViewModel”的模型项]'。
I believe this is saying my main view needs to have an IEnumerable of ConferenceTeamViewModel but then how do I pull data in without putting the entire page in a foreach loop?我相信这是说我的主视图需要有一个 ConferenceTeamViewModel 的 IEnumerable 但是我如何在不将整个页面放入 foreach 循环的情况下提取数据?
ConferenceTeamViewModel is just my viewmodel with team, conference, and fightsong data. ConferenceTeamViewModel 只是我的带有团队、会议和战斗数据的视图模型。
My current Details Action is:我当前的详细信息操作是:
public ActionResult Details( int id = 0)
{
var team = _db.Teams.Find(id);
var conference = _db.Conferences.FirstOrDefault(x => x.ConferenceId == team.ConferenceId);
if (team == null)
{
return HttpNotFound();
}
return View(new ConferenceTeamViewModel()
{
Teams = team,
Conference = conference
});
}
My Details View (I am able to pull in the single Model data, just not the fightsongs):我的详细信息视图(我能够提取单个模型数据,而不是战斗歌曲):
@model SportSongs.Models.ViewModels.ConferenceTeamViewModel
<div class="row">
<div class="col-sm-12">
@Model.Teams.School
</div>
</div>
<div class="row">
<div class="col-sm-12">
@Html.Partial("_FightSongDetails")
</div>
</div>
Here is my PartialView:这是我的部分视图:
@model IEnumerable<SportSongs.Models.ViewModels.ConferenceTeamViewModel>
<tbody id="fightsongPlaylist">
@foreach (var song in Model)
{
<tr>
<td>@song.Fightsong.songname</td>
<td><a href="@Url.Content(@song.Fightsong.songurl)" class="playbtn" onclick="myFunction()"><i data-songid="@Url.Content(@song.Fightsong.FightSongId.ToString())" class="fa fa-play"></i></a></td>
</tr>
}
It sounds like you want to show the songs for that team in a partial view.听起来您想在局部视图中显示该团队的歌曲。 If that's the case, make the model type for the partial view a collection of songs , not the team ).如果是这种情况,请将局部视图的模型类型设为歌曲集合,而不是团队)。
Change your partial view to将您的部分视图更改为
@model IEnumerable<FightSong>
<tbody id="fightsongPlaylist">
@foreach (var song in Model)
{
<tr>
<td>@song.songname</td>
<td><a href="@Url.Content(@song.songurl)" class="playbtn" onclick="myFunction()"><i data-songid="@Url.Content(@song.FightSongId.ToString())" class="fa fa-play"></i></a></td>
</tr>
}
and pass the collection to the sub-view:并将集合传递给子视图:
<div class="row">
<div class="col-sm-12">
@Html.Partial("_FightSongDetails",model.Teams.FightSongs)
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.