How to handle ViewData type casting in MVC

Posted by Wondering on Stack Overflow See other posts from Stack Overflow or by Wondering
Published on 2010-04-21T18:54:27Z Indexed on 2010/04/21 19:03 UTC
Read the original article Hit count: 183

Filed under:
|
|

Hi All, I am new to MVC and facing one issue. I have a xml file and i am retrieving its value using Linq to xml and assigning it to ViewData. Controller.cs

           var res=from x in doc.Descendants("person")
                  select new 
                  {
                    Fname=x.Element("fname").Value,
                    Lname=x.Element("lname").Value

                  };
                   ViewData["Persons"]=res;

in View I am trying

         <% foreach (var item in ViewData["Persons"])   
                 { %>  


                   <li> <%= item.Fname %> </li>  


                 <% } %>  

but foreach (var item in ViewData["Persons"] is giving type casting error..what should be the exact type csting so that i can retrive values in the format item.Fname.

Thanks.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about ASP.NET