【发布时间】:2021-09-05 14:22:58
【问题描述】:
不清楚如何将此模型传递给我的视图。
我有一个列表模型被传递到我的视图中。但我想根据在 ViewBag 上登录的 User 来分隔列表中的 Participant。
当前视图代码(工作):
此代码可以正常工作,但不会根据登录的用户来分隔参与者。它显示了整个列表。我在我的 foreach 行中的 @model、League 和我的 secondforeach 中的 MMLeagueParticipants 下得到了红色的 squilies。但它仍然有效。
错误:
当我将鼠标悬停在 @model 和 League 上时:
The type or namespace name 'League' could not be found (are you missing a using directive or an assembly reference?)[LeagueProect]
当我将鼠标悬停在 MMLeagueParticipant 上时: 找不到类型或命名空间名称“MMLeagueParticpant”(您是否缺少 using 指令或程序集引用?`
//RED SQUIGLY #1 @model
@model List<League>
@{
if (Model != null && Model.Any())
{
//RED SQUIGLY #2 League
foreach(League i in Model)
{
//RED SQUIGLY #3 MMLeagueParticipant
foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
}
else
{
<p>the list is empty</p>
}
}
尝试分离(无效)
仍在使用@model List<League>。
当我更改代码以尝试分离时,它会完全崩溃
<p>Your kid(s):</p>
if (Model != null && Model.Any())
{
foreach(League i in Model)
{
var userParticipants = i.allParticipants.Where(p => p?.child?.Parent?.UserId == ViewBag.UserId).ToList();
foreach (MMLeagueParticipant mmLeagueParticipant in userParticipants)
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
}
<p>not your kid(s):</p>
if (Model != null && Model.Any())
{
foreach(League i in Model)
{
var userParticipants = i.allParticipants.Where(p => p?.child?.Parent?.UserId != ViewBag.UserId).ToList();
foreach (MMLeagueParticipant mmLeagueParticipant in userParticipants)
{
<p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
}
}
}
我尝试这个得到的错误:
NullReferenceException: Object reference not set to an instance of an object.
它不喜欢这些线条:
var userParticipants = i.allParticipants.Where(p => p.child.Parent.UserId == ViewBag.UserId).ToList();@await Html.PartialAsync("_Displayeach", Model["bbM7and8"])
我假设我将模型传递给我的视图的方式不正确。但完全不确定为什么一种方法有效而另一种无效。
控制器详情:
-
在我的控制器中,我有一个年龄组列表,我添加了新的
participant模型。 -
然后我将列表传递给我的
RosterPageBasketball视图。 -
在该视图中,我有 12 个选项卡;每个根据年龄组显示不同的列表。
-
RosterPageBacketball 中使用的模型:
@model List<League> -
RosterPageBasketball 中显示不同年龄组的每个选项卡:
@await Html.PartialAsync("_Displayeach", Model["bbM7and8"]) -
_displayeach 是我的当前视图代码
当我不是显示所有带有我的当前视图代码(如上所示)的Participants 以及他们自己的不同年龄组列表时,这些选项卡可以根据需要工作 strong> 根据参与者是否属于登录用户来区分参与者。
我的RosterPageBasketball中的每个标签都包含以下代码:@await Html.PartialAsync("_Displayeach", Model["bbM#and#"])
我认为问题可能出在哪里:
("_Displayeach", Model["bbM7and8"])
我发送到我的部分模型是 Model["bbM7and8"],但我在 _Displayeach 中使用的模型是 @model List<League>。不确定如何或是否可以传入@model Dictionary<string, List<League>>。
控制者:
public async Task<IActionResult> GetBasketballRoster()
{
String[] leagueNames = new[]
{
"bbM7and8",
"bbM9and10",
"bbM11and12",
"bbM13and14",
"bbM15and16",
"bbM17and18",
"bbF7and8",
"bbF9and10",
"bbF11and12",
"bbF13and14",
"bbF15and16",
"bbF17and18"
};
Dictionary<string, List<League>> d = new Dictionary<string, List<League>>();
foreach (var name in leagueNames)
{
List<League> bbLeagues = await db.Leagues
.Where(l => l.sport == "Basketball")
.Where(l => l.ageRange==name)
.ToListAsync();
foreach (League league in bbLeagues)
{
List<MMLeagueParticipant> leagueParticipants = await db.Entry(league)
.Collection(l => l.allParticipants)
.Query() // <-- This is needed to allow for `Include()`
.Include(mmp => mmp.child)
.ToListAsync();
}
d.Add(name,bbLeagues);
}
return View("RosterPageBasketball", d);
}
- Dictionary
d 被传递给 RosterpageBasketball - _Displayeach 视图传递一个模型["bbM#and#"]
- _Displayeach 视图使用
@model List<League>
【问题讨论】:
标签: c# asp.net-mvc asp.net-core model-view-controller