2009-09-17 54 views
2

我不斷收到錯誤,試圖遍歷視圖中的ViewData ...我甚至強烈嘗試向IEnumerable(App.Models.Namespace)鍵入視圖並使用Model,無濟於事。要麼因爲缺少GetEnumerable方法或無效類型轉換而出現錯誤...任何想法我如何做到這一點?爲什麼我無法通過ViewData或Model進行預覽?

型號...

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> 

回答

13

跟投試試這個:

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

嘗試已經,得到了一個無效的轉換錯誤 – BigOmega 2009-09-17 21:33:10

+1

找出計算機[ 「產品」]真的是通過輸出 計算機[ 「產品」。的GetType( )。全名 – 2009-09-17 22:27:34

0
<%foreach(Product prod in ViewData["products"]){%> 
+1

'var'是一個隱式類型聲明,意味着當你編寫代碼時你不必知道變量的確切類型。 – ChrisF 2009-09-17 20:52:19

+1

正確,但只有當返回的結果是確切類型時。在ViewData的情況下,我很確定它返回一個必須投射的對象。 Foreach正在爲你做演員 – mxmissile 2009-09-17 20:58:58

0
foreach(var prod in ViewData["products"] as IQueryable<Product>) 
1

無論如何你爲什麼要這樣做?爲什麼不這樣做:

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

在您的看法。然後在你的控制器動作:

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

然後,你根本不必使用ViewData

2
foreach (var prod in (ViewData["products"] as IEnumerable<Product>)) 

我陷入了類似的情況,這爲我工作,希望幫助.. :)

感謝, 馬赫什Velaga

0

如果從兩個以上的模型越來越列表想要在一個列表中顯示兩個模型,而不是像這樣使用.. 首先創建您的兩個模型是Student,其次是Teacher。

// 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> 
} 
相關問題