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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:25:30+00:00 2026-05-19T09:25:30+00:00

System.InvalidCastException iterating through Viewdata I need to replace the code <%=Html.DropDownList(Part, (SelectList)ViewData[Parts])%> for dropdown

  • 0

System.InvalidCastException iterating through Viewdata

I need to replace the code

"<%=Html.DropDownList("Part", (SelectList)ViewData["Parts"])%>

for dropdown in the following manner for some reason.

 <select> <% foreach (Hexsolve.Data.BusinessObjects.HSPartList item in (IEnumerable<Hexsolve.Data.BusinessObjects.HSPartList>)ViewData["Parts"])
               { %>
            <option value="<%=item.Id %>">
                <%=item.PartName %>
                &nbsp;&nbsp;&nbsp;
                <%=item.IssueNo %></option>
            <% } %>
        </select>

I am getting error converting SelectedList to IEnumerable)

Error: Unable to cast object of type ‘System.Web.Mvc.SelectList’ to type ‘System.Collections.Generic.IEnumerable`1[Hexsolve.Data.BusinessObjects.HSPartList]’.

Is this the right way to iterate through viewdata[]. Please help me out of this.

  • 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-19T09:25:30+00:00Added an answer on May 19, 2026 at 9:25 am

    Sorry my previous answer was incorrect.

    I believe the problem is that GetEnumerator returns IEnumerator<SelectListItem>, not IEnumerator<Hexsolve.Data.BusinessObjects.HSPartList> which you are trying to access.

    I would suggest something like the following should work:

    <% foreach (SelectListItem item in (SelectList)ViewData["Parts"])
    { %>
        <option value="<%= item.Value %>"><%= item.Text %></option>
    <% } %>
    

    If you need to concatenate PartName and IssueNo in the Text property which it looks like you’re trying to do, you could do that while building the select list.

    So you might end up with something like:

     public class Part
     {
         public int Id { get; set; }
         public int IssueNo { get; set; }
         public string PartName { get; set; }
     }
    

    and

       var parts = new List<Part>{
                 new Part { Id = 1, IssueNo = 1, PartName = "Spanner" },
                 new Part { Id = 2, IssueNo = 1, PartName = "Hammer" }
             };
    
       var selectList = new SelectList(
             parts.Select(p => 
                    new { Id = p.Id, 
                          Name = p.PartName + " " + p.IssueNo.ToString()}),
             "Id",
             "Name");
    
       foreach (var item in selectList)
       {
           Console.WriteLine(item.Value);
           Console.WriteLine(item.Text);
       }
    
       /* 
       Output:
       1
       Hammer 1
       2
       Spanner 1
       */
    

    Hope that helps!

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

Sidebar

Related Questions

Why I'm getting this error? System.InvalidCastException was unhandled by user code Message=Specified cast is
Our system dynamically generates a large report by converting many HTML pages into a
I run into this problem System.InvalidCastException: Unable to cast object of type 'System.Data.Linq.DataQuery 1[Student]'
The following code fails at runtime… Dim Id As Guid = CType(e.CommandArgument, Guid) It
The tooltip displayed for Convert.ToDateTime() says that it always throws System.InvalidCastException. Why?
ArgumentException argumentException = (ArgumentException)new Exception(); throws: System.InvalidCastException: Unable to cast object of type 'System.Exception'
I have the following code: int intNumber1 = 100; object intNumber2 = 100; bool
I am intermittently getting an System.InvalidCastException: Specified cast is not valid. error in my
System.InvalidCastException was unhandled Message=Unable to cast object of type 'SportSoft.SSBLL.CMembershipTypeAdvanceBooking' to type 'SportSoft.SSBLL.CAreaActivities'. Source=SSDlgs
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to

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.