Why can't I go through my ViewData or Model?

I keep getting errors trying to iterate through the ViewData in the view there ... I even tried hard typing the IEnumerable view (App.Models.Namespace) and using the model, to no avail. Either I am getting an error for a missing GetEnumerable method or an invalid cast type ... Any idea how I do this?

Model ...

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

      

controller...

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

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

    return View();
}

      

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>

      

+2


source to share


6 answers


Try this with:



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

      

+13


source


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

      



I got into a similar situation and it worked for me.

+2


source


Why are you doing it like that? Why not do:

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

      

in your view. Then, in your controller action, do:

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

      

Then you don't need to use at all ViewData

.

+1


source


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

      

0


source


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

      

0


source


If you are getting a list of more than two models and want to show two models in one list, you should use that. First, create two models: Student and second, 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; }
}

      

// Create a controller

  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>
}

      

0


source







All Articles