【问题标题】:Pass ViewBag with List to view for foreach loop使用 List 传递 ViewBag 以查看 foreach 循环
【发布时间】:2016-05-13 04:05:24
【问题描述】:

我通过 Viewbag 将查询结果从控制器传递到视图,但是当循环通过 Viewbag 结果时,我得到错误“对象”不包含“评级”的定义,即使它在调试时确实出现了。我没有使用模型进行查询,因此无法转换列表。

如何将查询中的列表或查询结果本身发送到视图,以便循环并提取数据?

控制器

var AppQuery = (from ans in db.Answers
                        join ques in db.Questions on ans.QuestionID equals ques.QuestionID
                        join resp in db.Responses on ans.ResponseID equals resp.ResponseID
                        join sec in db.Sections on ques.SectionID equals sec.SectionID
                        where resp.ResponseID == ID && ques.SubSectionName != null
                        select new { SectionName = sec.SectionName, RatingAnswer = ans.RatingAnswer })
 .GroupBy(a => a.SectionName)
 .Select(a => new { SectionName = a.Key, Ratings = a.Sum(s => s.RatingAnswer) });


        ViewBag.Results = AppQuery.ToList();

查看

    @{var Results = ViewBag.Results;}

 @foreach (var item in Results)
                                        {
                                            var style = "active";
                                            var condition = "";

                                            if (item.Ratings >= 90)
                                            {
                                                style = "success";
                                                condition = "Excellent";
                                            }
                                            else if (item.Ratings < 50)
                                            {
                                                style = "danger";
                                                condition = "Critical";
                                            }
                                            else
                                            {
                                                style = "active";
                                                condition = "Stable";
                                            }
                                            <tr class="@style">
                                                <td>@item.SectionName</td>
                                                <td>@item.Ratings</td>
                                                <td>@condition</td>

                                            </tr>

                                        }

感谢您的帮助!

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-5 viewbag


    【解决方案1】:

    这是预期的行为!。因为您为 ViewBag 设置了一组匿名对象。

    您应该使用强类型视图模型。

    public class RatingVm
    {
      public string SectionName { set;get;}
      public int Ratings { set;get;}
    }
    

    在你的行动方法中,

    public ActionResult Create()
    {
      var results= (from ans in db.Answers
                         join ques in db.Questions on ans.QuestionID equals ques.QuestionID
                         join resp in db.Responses on ans.ResponseID equals resp.ResponseID
                         join sec in db.Sections on ques.SectionID equals sec.SectionID
                         where resp.ResponseID == ID && ques.SubSectionName != null
                            select new { SectionName = sec.SectionName, 
                                         RatingAnswer = ans.RatingAnswer })
     .GroupBy(a => a.SectionName)
     .Select(a => new RatingVm { SectionName = a.Key, 
                                 Ratings = a.Sum(s => s.RatingAnswer) }).ToList();
      return View(results);
    
    }
    

    现在您的视图应该被强类型化为我们从操作方法传递的数据类型,它是RatingVm 的集合

    @model List<RatingVm>
    <h1>Ratings</h1>
    @foreach(var item in Model)
    {
      <p>@item.SectionName</p>
      <p>@item.Ratings</p>
    }
    

    【讨论】:

    • 啊,简单到让我觉得自己很愚蠢。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多