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 8843859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:20:27+00:00 2026-06-14T11:20:27+00:00

When trying to loop through data in a View that is returned from a

  • 0

When trying to loop through data in a View that is returned from a controller, I get an error ” ‘object’ does not contain a definition for ‘CustomerID’ and no extension method ‘CustomerID’ accepting a first argument of type ‘object’ could be found”

Here is my view

<% using (Html.BeginForm()) {%>

    <%foreach (var item in (IEnumerable)Model)
      { %>
      <%= Html.Encode(item.CustomerID) %>

    <%} %>

 <% } %>  

Here is the controller:

public ActionResult Index()
        {
            Models.NorthwindDataContext nw = new Models.NorthwindDataContext();

            var qry = from ord in nw.Orders
               join cust in nw.Customers on ord.CustomerID equals cust.CustomerID
               select new Models.OrdersModel
                      {
                          CustomerID = ord.CustomerID,
                          OrderID = ord.OrderID,                          
                          OrderDate = ord.OrderDate.Value,
                          ShipCountry = ord.ShipCountry
                      };
            var ordrs = qry.ToList();
                          return View(ordrs);

        } 

and here is my class

public class OrdersModel
    {
        [Required]
        [Display(Name = "OrderID")]
        public int OrderID { get; set; }

        [Required]   
        [Display(Name = "OrderDate")]
        public DateTime OrderDate { get; set; }

        [Required]
        [Display(Name = "CustomerID")]
        public string CustomerID { get; set; }

        [Display(Name = "ShipCountry")]
        public string ShipCountry { get; set; }  
    } 
  • 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-06-14T11:20:28+00:00Added an answer on June 14, 2026 at 11:20 am
    <%foreach (var item in (IEnumerable<OrdersModel>)Model)
          { %>
          <%= Html.Encode(item.CustomerID) %>
    
        <%} %>
    

    The CustomerID is not turning up because you’re not providing a type to your IEnumerable cast.

    .NET does the best it can and treats each item as an object, which does not contain the CustomerID property.

    EDIT:

    Inspired by Jan’s comment, this is the way I’d actually go about it.

    Create a better model, where Orders is a property of the view model.

    public class OrderListVM 
    {
      public List<OrdersModel> Orders { get; set; }
    
      // The benefit of using this model is you can include extra stuff here.
    }
    

    Then in your controller:-

    var vm = new OrderListVM { Orders = qry.ToList() };
    
    return View(vm);
    

    As Jan says, make sure the view is strongly-typed, and ready to accept OrdersListVM as its type.

    You can check this by looking at the page directive in your .aspx page.

    <%@ Page Title="" 
        Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<YourNameSpace.OrderListVM>" %>
    

    Finally, change your markup to:-

    <%foreach (var item in Model.Orders)
          { %>
          <%= Html.Encode(item.CustomerID) %>
    
        <%} %>
    

    Everything will have the right type, and if you need to put anything else on your view apart from a bare list of orders, you have a container to place it in.

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

Sidebar

Related Questions

I'm trying to loop through the results from the Last.fm API but it's not
I'm trying to create a script that will loop through some data frames and
I'm trying to loop through a list of results from MySQL and display all
I'm trying to loop through objects from my database in the template and one
I'm trying to loop through a number of items, and create a json object.
I'm trying to get simplepie to loop through a few rss feeds using codeigniter
Possible Duplicate: Loop through Json object { data: [ { name: Jen, id: 1
Im trying to loop through a json files' object array to access its variables'
I am moving from VB to C#. I am trying to loop through a
I'm trying to loop through a database and output data into a div, if

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.