【问题标题】:Why can't I foreach through my ViewData or Model?为什么我不能通过我的 ViewData 或 Model foreach?
【发布时间】:2010-11-29 07:10:04
【问题描述】:

我试图在视图中迭代我的 ViewData 时不断出错...我什至尝试将视图强输入到 IEnumerable(App.Models.Namespace) 并使用 Model,但无济于事。由于缺少 GetEnumerable 方法或无效的类型转换,我得到一个错误...知道我是怎么做的吗?

型号...

public IQueryable<Product> getAllProducts()
{
    return (from p in db.Products select p);
}

控制器...

public ActionResult Pricing()
{
    IQueryable<Product> products = orderRepository.getAllProducts();

    ViewData["products"] = products.ToList();

    return View();
}

查看...

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Pricing
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Pricing</h2>

    <div>
        <select class="product">
            <%foreach(var prod in ViewData["products"]){%>
                <option><%=prod.Title %></option>
            <%} %>

        </select><select></select>
    </div>

</asp:Content>

【问题讨论】:

    标签: asp.net-mvc ienumerable


    【解决方案1】:

    试试这个演员阵容:

    foreach(var prod in (List<Product>)ViewData["products"])
    

    【讨论】:

    • 通过输出 ViewData["products"].GetType().FullName 找出 ViewData["products"] 的真正含义
    【解决方案2】:
    foreach (var prod in (ViewData["products"] as IEnumerable<Product>))
    

    我遇到了类似的情况,这对我有用。

    【讨论】:

      【解决方案3】:

      你为什么要这样做呢?为什么不这样做:

      Inherits="System.Web.Mvc.ViewPage<IEnumerable<Product>>
      

      在你看来。然后在您的控制器操作中执行:

      public ActionResult Pricing()
      {
          IQueryable<Product> products = orderRepository.getAllProducts();
          return View(products.ToList(););
      }
      

      那你根本不用ViewData

      【讨论】:

        【解决方案4】:
        <%foreach(Product prod in ViewData["products"]){%>
        

        【讨论】:

        • var 是一个隐式类型声明,意味着您在编写代码时不必知道变量的确切类型。
        • 正确,但仅当返回结果为精确类型时。对于 ViewData,我很确定它返回一个必须强制转换的对象。 Foreach 正在为你做演员
        【解决方案5】:
        foreach(var prod in ViewData["products"] as IQueryable<Product>)
        

        【讨论】:

          【解决方案6】:

          如果您从两个以上的模型中获取列表并希望在一个列表中显示两个模型,那么您应该像这样使用.. 创建您的两个模型,首先是学生,其次是教师。

          // Create Models
          public class Student
          {
              public int Id { get; set; }
              public string Name { get; set; }
              public string Address { get; set; }
              public List<int> Marks { get; set; }
          }
          
          public class Teacher
          {
              public int Id { get; set; }
              public string Name { get; set; }
              public string Address { get; set; }
          }
          

          //创建控制器

            public ActionResult Index()
              {
                  List<Student> students = new List<Student>()
                  {
                      new Student {Id = 1, Name = "Vikas", Address = "Mohali", Marks = new List<int> {90, 23, 46,56} },
                      new Student {Id = 2, Name = "Rajeev", Address = "Mohali", Marks = new List<int> { 56, 78, 34, 67 }},
                      new Student {Id = 3, Name = "Ajay", Address = "Delhi", Marks = new List<int> {56, 78, 34, 56}}
                  };
          
                  List<Teacher> teachers = new List<Teacher>()
                  {
                      new Teacher {Id = 1, Name = "Arun Nagar", Address = "Delhi"},
                      new Teacher {Id = 2, Name = "Manish Kumar", Address = "Mohali"}
                  };
          
                  var Querylist = (from student in students
                                   where student.Address == "Mohali"
                                   select student.Name)
                                  .Concat(from teacher in teachers
                                          where teacher.Address == "Mohali"
                                          select teacher.Name);
                  //get list in ViewBag
                  ViewBag.DataLIst = Querylist;
                  //get list in View Data
                  ViewData["DataLIst1"] = Querylist.ToList();
                  return View(Querylist.AsEnumerable());
              }
          
          //create View "Index.cshtml"   
          
          
          @foreach (var h in @ViewBag.DataLIst)
          { 
              <h3>@h</h3>
          }
          @foreach (var s in @ViewData["DataLIst1"] as List< string>)
          {
              <h1>@s</h1>
          }
          

          【讨论】:

            猜你喜欢
            • 2014-12-01
            • 2019-10-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-07
            • 2020-06-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多