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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:17:22+00:00 2026-06-06T12:17:22+00:00

I have just started with my project using MVC and Razor. Now I am

  • 0

I have just started with my project using MVC and Razor. Now I am encountering a problem when it comes to binding data coming from the database to a dropdownlist. Please refer on my codes below:

Specialization Model:

public class SpecializationModel
{
    [Display(Name = "SpecializationID")]
    public string SpecializationID { get; set; }

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

    public IEnumerable<SelectListItem> Items { get; set; }
    public int SelectedSpecializationID { get; set; }
}

Controller:

public ActionResult Physicians()
{
    SpecializationManager spec = new SpecializationManager();
    List<Specialization> SpecializationList = spec.GetAllSpecialization();

    var obj = new SpecializationModel();

    obj.Items = new[]
    {
        foreach(var x in SpecializationList)
        {
            new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription };
        }
    };

    return View(obj);
}

I have this manager which contains my LINQ query to extract the data from the database.
I encounter problems on the controller. Wherein the error points on the foreach syntax saying Invalid expression term foreach

Can anyone please point me to the right direction? Thanks a lot!

EDIT:

I have this code now without errors on the foreach part (thanks to the post below which I combined with what I have above). However, I can’t seem to make the last line work. It produces an error about implicit cast:

var items = new List<SelectListItem>();
foreach (var x in SpecializationList)
{
    items.Add(new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription });
}

obj.Items = items.ToList();

Please do help me. 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-06-06T12:17:25+00:00Added an answer on June 6, 2026 at 12:17 pm

    yopu can’t put a foreach in a constructor, try:

    var items = new List<SelectListItem >();
    foreach(var x in SpecializationList)
    {
               items.add(new SelectListItem { Value = x.SpecializationID.ToString(), Text = x.SpecializationDescription });
    }
    
    obj.Items = items;
    

    Edited

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

Sidebar

Related Questions

I have just started using visualsvn (SVN in general, rather) coming from TFS 2010
I just started an MVC 3 project using Forms Authentication. I have a web
I have just started a project that involves me sending data using POST in
I have just started a new project, and I am using the Delphi 2009
I just started using CakePHP for a small project. I have rails experience, and
I have just started using the AsyncController in my project to take care of
I'm currently working on a school project in Eclipse (We have just started using
We've just started using Sencha's ExtJS 4 in our project and have been following
I have just started a new project using a Linq to Sql model and
I have just started using and am falling in love with MVC design pattern.

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.