【问题标题】:How to append values to ViewBag in controller .net core MVC?如何将值附加到控制器 .net 核心 MVC 中的 ViewBag?
【发布时间】:2020-11-02 05:33:06
【问题描述】:

我的控制器中有一个加载 ViewBag,它从我的 SQL 数据库中获取数据,如下所示。

ViewBag.students = context.Gen_list.Where(d => d.AppRole == "Leader")
                .Select(d => d.FullName).Distinct();

执行上述命令后,ViewBag.students 有 2 个值,如下所示

[0] "Mike Igar"
[1] "Ram Barb"

现在我想在控制器中附加 2 个值,以便 ViewBag.students 如下所示,

[0] "Mike Igar"
[1] "Ram Barb"
[2] "Krish Sag"
[3] "Paul Dev"

我搜索了 2 个多小时,但无法弄清楚如何获得上述结果。您的帮助将不胜感激。谢谢。

【问题讨论】:

    标签: .net asp.net-mvc asp.net-core c#-4.0


    【解决方案1】:

    找到了解决办法。我没有尝试将列表添加到 ViewBag.Below,而是将列表添加到变量中,而不是将其提供给 ViewBag。

       var consultants = context.Gen_list.Where(d => d.AppRole == "Leader")
                     .Select(d => d.FullName).Distinct();ToList();
    
       List<string> weekend = new List<string> {"Krish Sag", "Paul Dev"};
        
       ViewBag.students = students.Concat(weekend);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2012-01-11
      相关资源
      最近更新 更多