Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 734061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:19:43+00:00 2026-05-14T07:19:43+00:00

I am new to MVC and facing one issue. I have a xml file

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:19:43+00:00Added an answer on May 14, 2026 at 7:19 am

    Using ViewData, although possible, is not the most elegant way to pass information from controller to view. Using specific view model instead makes code easier and cleaner, and resolves casting issues.

    Try defining model first:

    public class ViewModelElement
    {
        public string Fname { get; set; }
        public string LName { get; set; }
    }
    
    
    public class ViewModel
    {
        public List<ViewModelElement> Elements { get; set; };
    }
    

    Then use your model in action:

    public ActionResult ActionName()
    {
      //get doc somehow
      var model = new ViewModel();
    
      //When querying by linq to xml, you can create ViewModelElement instead of anonymous class
      model.Elements = (from x in doc.Descendants("person")
          select new ViewModelElement
          {
            Fname=x.Element("fname").Value,
            Lname=x.Element("lname").Value
          }).ToList();
    
      return View(model);
    }
    

    Then use your model in view:

    <% foreach (var item in model.Elements) { %>  
       <li> <%= item.Fname %> </li>  
    <% } %> 
    

    View has to inherit from System.Web.Mvc.ViewPage<ViewModel>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m new to MVC I am facing one problem for finding control of
Set up: New MVC 3 project, using existing database. Created an EF file (edmx),
I have started a new mvc project and clicked on project\properties\web - use local
I've just create a new MVC project, and have made no changes at all,
Just starting a new asp.net mvc solution and one of the requirements is the
I have a new MVC 2 site that has been working fine on my
I have a problem in my application I am using MVC 3 with razor
I have a new MVC application which integrates into a larger pre-existing intranet site.
i'm new to ASP.NET MVC and i'm facing some structural design problems. I can't
I have a new MVC 4 Application with a fairly basic View/Controller. The associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.