1

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 :)

Smiley
  • 3,207
  • 13
  • 49
  • 66

1 Answers1

1

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

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Hi! Thanks for the response. But your code seems to have errors. On the `items.Add(x)` part, the compiler says that it has some invalid arguments. Also, `items.ToArray()` produces an error about implicit conversion of SelectListItem to IEnumerable. – Smiley Jun 27 '12 at 13:12
  • If that doesn't work you need to let me know what obj.Items is! – Liam Jun 27 '12 at 13:26
  • I already tried that one too. I think the problem is that obj.Items is of type IEnumerable and we are trying to put items of type List to it. I also tried casting items by doing `obj.Items = (IEnumerable)items` but it still won't work. :( – Smiley Jun 27 '12 at 13:28
  • It's on the first code block above. Declared as: `public IEnumerable Items { get; set; }` – Smiley Jun 27 '12 at 13:31
  • A List inherits IEnumerable so that doesn't make sense? Just wrote the following to check: List test2 = new List(); IEnumerable test = test2; – Liam Jun 27 '12 at 13:31
  • OK, I think you must have different SelectListItem classes? Check the full namespaces match? – Liam Jun 27 '12 at 13:33
  • You're right. That code you asked me to write works. Here's the exact error message I'm getting: – Smiley Jun 27 '12 at 13:38
  • You're right. That code you asked me to write works. Here's the exact error message I'm getting: `Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)` – Smiley Jun 27 '12 at 13:38
  • 1
    OHHHHHHHHHHHHHHH! I got it! Apparently, I mistakenly wrote `using System.Web.WebPages.Html;` instead of `using System.Web.Mvc;` to declare the IEnumerable. Thank you for your help!!!! – Smiley Jun 27 '12 at 13:40