将 ViewBag 与 List 一起传递以查看 foreach 循环

Pass ViewBag with List to view for foreach loop

我正在通过 Viewbag 将查询结果从控制器传递到视图,但是在遍历 Viewbag 结果时我收到错误 'object' does not contain a definition for 'Ratings' 即使它调试时确实显示。我没有使用模型进行查询,因此无法投射列表。

如何将列表从查询或查询结果本身发送到视图,以便遍历并提取数据?

控制器

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>

                                        }

感谢您的帮助!

这是预期的行为!。因为您将匿名对象的集合设置为您的 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>
}