-2

I try to populate DropDownList from my database, but still have error

Cannon implicity convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.IEnumerable<MyWeb.Models.HomeViewModels.SelectListItem>'

My model:

 public class BazarInsertViewModel
        {           
            public int SelectedCategoryId { get; set; }
            public IEnumerable<SelectListItem> MyCategory { get; set; }
        }
        public class SelectListItem
        {
            public string Value { get; set; }
            public string Text { get; set; }
        }

and my controller:

public ActionResult BazarInsert()
        {
            var model = new Models.HomeViewModels.BazarInsertViewModel
            {
                MyCategory = GetCategory()
            };
            return View(model);
        }
  private IEnumerable<Models.HomeViewModels.SelectListItem> GetCategory()
        {
            var dbo = new WebEntities();
            var category = dbo
                        .bazar
                        .Select(x =>
                                new Models.HomeViewModels.SelectListItem
                                {
                                    Value = x.ID.ToString(),
                                    Text = x.TITLE
                                });

            return new SelectList(category, "Value", "Text");
        }

Please can you explain me what I'm doing wrong?

Martin
  • 77
  • 2
  • 12

5 Answers5

2

By returning category.ToList() it'll be fine. I'm using it in one of my projects and it works fine.

private IEnumerable<System.Web.Mvc.SelectListItem> GetCategory()
{
    var dbo = new WebEntities();
    var category = dbo
                    .bazar
                    .Select(x =>
                            new System.Web.Mvc.SelectListItem
                            {
                                Value = x.ID.ToString(),
                                Text = x.TITLE
                            });
     return category.ToList();
}
dotnetspark
  • 551
  • 4
  • 23
  • So is there other problem .. in view is ` @Html.DropDownListFor(model => model.SelectedCategoryId, new SelectList(Model.MyCategory, "Value", "Text"), "-----Select-----", htmlAttributes: new { @class = "form-control" })` but still I have a error _Value cannot be null. Parameter name: items_ – Martin Oct 19 '16 at 10:08
  • Use this instead... @Html.DropDownListFor(model => model.SelectedCategoryId, Model.MyCategory, "-----Select-----", new { @class = "form-control" }) – dotnetspark Oct 19 '16 at 12:50
1

Your method is IEnumerable<Models.HomeViewModels.SelectListItem> ! and your code returns SelectList SelectList cannot be converted to your custom SelectListItem class (without an explicit cast)

FYI : There is already a SelectListItem class in System.Wb.Mvc. Why not use that ?

private IEnumerable<System.Web.Mvc.SelectListItem> GetCategory()
{
    var dbo = new WebEntities();
    var category = dbo
                    .bazar
                    .Select(x =>
                            new System.Web.Mvc.SelectListItem
                            {
                                Value = x.ID.ToString(),
                                Text = x.TITLE
                            }).ToList();
     return category;
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

You need to pass

  new SelectList(category.Select(x => new{Value=x.Value,Text=x.Text}).ToList(), "Value", "Text");

I give you just hint for initializing List but please to get object of class please write full class name.

Niraj
  • 775
  • 7
  • 20
-1

In GetCategory() you return a SelectList, but your method signature states IEnumerable.

Change your model property to a SelectList:

public class BazarInsertViewModel
{
    public int SelectedCategoryId { get; set; }
    public SelectList MyCategory { get; set; }
}
public class SelectListItem
{
    public string Value { get; set; }
    public string Text { get; set; }
}

And the method signature to return a SelectList

public ActionResult BazarInsert()
        {
            var model = new BazarInsertViewModel
            {
                MyCategory = GetCategory()
            };
            return View(model);
        }
        private SelectList GetCategory()
        {
            var dbo = new WebEntities();
            var category = dbo
                        .bazar
                        .Select(x =>
                                new Models.HomeViewModels.SelectListItem
                                {
                                    Value = x.ID.ToString(),
                                    Text = x.TITLE
                                });

            return new SelectList(category, "Value", "Text");
        }
Adriaan
  • 1
  • 5
  • He needs a IEnumerable of SelectList to use in a the Razor view. So it should be the other way around. He needs to return a list not a single item – Liam Oct 18 '16 at 14:33
-1

Take a look at GetCategory() you are supposed to return a list of type IEnumerable<SelectListItem>. Instead you are returning a single item of type SelectList.

If can change your code, check another solution here or other approaches to almost similar question.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
iAM
  • 1,365
  • 15
  • 25
  • Put all relevant code in your answer please. Don't say the answer is here. If you beleiev this to be a duplicate flag it as such – Liam Oct 18 '16 at 14:34