Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc. Passing a list via viewData

Hi does anyone know how to pass a list throught the "ViewData". This is what I'm trying but I think I'm missing a cast some where.

List<GalleryModel> galleryList = new List<GalleryModel>();
        galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn1.com" });
        galleryList.Add(new GalleryModel() { isApproved = true, uri = "www.cnn2.com" });

        ViewData["SomeList"] = galleryList;

here's my aspx page code:

 <% List<myNS.CM.AVDTalentApplication.Models.GalleryModel> galList = ViewData["SomeList"];  %>
<% foreach (var gal in galList) { %>
<%= gal.uri%>
<%} %>
like image 807
RayLoveless Avatar asked Nov 08 '10 16:11

RayLoveless


Video Answer


2 Answers

You need to cast it in the view:

<% var galList = ViewData["SomeList"] as List<myNS.CM.AVDTalentApplication.Models.GalleryModel>;  %>

or

<% var galList = (List<myNS.CM.AVDTalentApplication.Models.GalleryModel>) ViewData["SomeList"];  %>
like image 90
Nasir Avatar answered Sep 28 '22 20:09

Nasir


For this line:

List<myNS.CM.AVDTalentApplication.Models.GalleryModel> galList = ViewData["SomeList"];

change it to

var galList = ViewData["SomeList"] as List<myNS.CM.AVDTalentApplication.Models.GalleryModel>;
like image 42
Daniel A. White Avatar answered Sep 28 '22 18:09

Daniel A. White