【问题标题】:passing a Dictionary<string, List<model>> model to a view将 Dictionary<string, List<model>> 模型传递给视图
【发布时间】:2021-09-05 14:22:58
【问题描述】:

不清楚如何将此模型传递给我的视图。 我有一个列表模型被传递到我的视图中。但我想根据在 ViewBag 上登录的 User 来分隔列表中的 Participant

当前视图代码(工作): 此代码可以正常工作,但不会根据登录的用户来分隔参与者。它显示了整个列表。我在我的 foreach 行中的 @modelLeague 和我的 secondforeach 中的 MMLeagueParticipants 下得到了红色的 squilies。但它仍然有效。

错误:

当我将鼠标悬停在 @modelLeague 上时: 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&lt;League&gt;。 当我更改代码以尝试分离时,它会完全崩溃

    <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 =&gt; p.child.Parent.UserId == ViewBag.UserId).ToList();
  • @await Html.PartialAsync("_Displayeach", Model["bbM7and8"])

我假设我将模型传递给我的视图的方式不正确。但完全不确定为什么一种方法有效而另一种无效。

控制器详情:

  • 在我的控制器中,我有一个年龄组列表,我添加了新的participant 模型。

  • 然后我将列表传递给我的RosterPageBasketball 视图。

  • 在该视图中,我有 12 个选项卡;每个根据年龄组显示不同的列表。

  • RosterPageBacketball 中使用的模型:@model List&lt;League&gt;

  • 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&lt;League&gt;。不确定如何或是否可以传入@model Dictionary&lt;string, List&lt;League&gt;&gt;


控制者:

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&lt;League&gt;

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core model-view-controller


    【解决方案1】:

    从您的代码来看,您似乎想显示特定ageRange 的部分视图中的List&lt;League&gt;

    这是一个您可以关注的工作演示:

    型号:

    public class League
    {
        public string sport { get; set; }
        public string ageRange { get; set; }
        public List<MMLeagueParticipant> allParticipants { get; set; }
    }
    public class MMLeagueParticipant
    {
        public child child { get; set; }
    }
    public class child
    {
        public string ParticipantFirstName { get; set; }
        public string ParticipantLastName { get; set; }
    }
    

    RosterPageBasketball.cshtml:

    @model Dictionary<string, List<League>>
    
    @{ 
        var data = Model.Where(a => a.Key == "bbF17and18").Select(a => a.Value).FirstOrDefault();
    }
    @await Html.PartialAsync("_Displayeach", data)
    

    _Displayeach.cshtml:

    @model List<League>
    @{
        if (Model != null && Model.Any())
        {
            foreach (League i in Model)
            {
                foreach (MMLeagueParticipant mmLeagueParticipant in i.allParticipants)
                {
                    <p>@mmLeagueParticipant.child.ParticipantFirstName @mmLeagueParticipant.child.ParticipantLastName</p>
                }
            }
        }
        else
        {
            <p>the list is empty</p>
        }
    }
    

    控制器:

    public async Task<IActionResult> Index()
    {
        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)
        {
            //hard coded the data....
            List<League> bbLeagues = new List<League>()
            {
                new League(){sport="Basketball",ageRange="bbF17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="San",ParticipantLastName="Da" } } } },
                new League(){sport="Basketball",ageRange="bbF17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Api",ParticipantLastName="Ee" } } }},
                new League(){sport="Basketball",ageRange="bbF9and10",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="May",ParticipantLastName="Fa" } } }},
                new League(){sport="Basketball",ageRange="bbM17and18",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Ben",ParticipantLastName="He" } } }},
                new League(){sport="Basketball",ageRange="bbF15and16",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Jun",ParticipantLastName="Pa" } } }},
                new League(){sport="FootBall",ageRange="bbF15and16",allParticipants =new List<MMLeagueParticipant>(){ new MMLeagueParticipant() { child= new child() {ParticipantFirstName="Pen",ParticipantLastName="Me" } } }}
            };
    
            var data = bbLeagues.Where(l => l.sport == "Basketball").Where(l => l.ageRange == name).ToList();
            d.Add(name, data);
        }
        return View("Index", d);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 2012-12-02
      • 1970-01-01
      • 2021-10-04
      • 2012-05-13
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多