1

ViewModel:

public class GroepModel
    {
        public int id { get; set; }
        public String Naam { get; set; }
        public String Beschrijving { get; set; }
    }

Controller:

public class GroepController : Controller
{
    AlinaDatabaseDataContext db = new AlinaDatabaseDataContext();
    // GET: Groep
    public ActionResult Groepen()
    {
        List<GroepModel> groepen = Mapper.Map<List<GroepenWerkvorm>, List<GroepModel>>(db.GroepenWerkvorms.ToList());

        return View(groepen);
    }
}

View

@model alina1617.Models.GroepModel

@{
    ViewBag.Title = "Groepen";
}

<h2>Groepen</h2>
<div>
    @Html.DropDownListFor(model => model. //This is not working  )
</div>

I've looked around and a lot of the things I'm finding solve this using ViewBags, but isn't it suboptimal to use them? So what would be the best approach to get a dropdownlist using a model class with data from a database?

5
  • 2
    First off, your view uses a property called Name, but in your GroepModel you do not have a property called Name Commented Dec 21, 2016 at 13:56
  • Doesn't generate anything when I type model. (so no properties are even showing), hence why I accidently typed name, it's not working at all though. Basically it says 'no overload for DropDownListFor takes 0 arguments. Commented Dec 21, 2016 at 13:59
  • Can it be from your DatabaseContext class? how is it connected? Commented Dec 21, 2016 at 14:02
  • 1
    use @model IEnumerable<alina1617.Models.GroepModel> when returning the list items and retrieve the items using @foreach(var item in Model) Commented Dec 21, 2016 at 14:02
  • your dropdown requires IEnemurable<SelectListItem> type.. But you are providing your custom type.. Commented Dec 21, 2016 at 14:04

1 Answer 1

3

first you need to add an SelectList to your viewModel :

public class MyViewModel {
   public SelectList list {get;set;}
   public int selectedItem {get;set;}
}

then you need to add your list to the SelectList :

public class GroepController : Controller
{
    AlinaDatabaseDataContext db = new AlinaDatabaseDataContext();
    // GET: Groep
    public ActionResult Groepen()
    {
        List<GroepModel> groepen = Mapper.Map<List<GroepenWerkvorm>,     List<GroepModel>>(db.GroepenWerkvorms.ToList());

        var model = new MyViewModel();

        model.list = new SelectList(groepen, "id", "Naam");

        return View(model);
    }
}

and in the view :

@model alina1617.Models.MyViewModel

@{
    ViewBag.Title = "Groepen";
}

<h2>Groepen</h2>
<div>
    @Html.DropDownListFor(model => model.selectedItem, Model.list  )
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Quick question, what if I DO put it in a viewbag (because I need multiple dropdownlists on a single view). I tried: @Html.DropDownListFor(model => model.selectedItem, ViewBag.model.list,"Selecteer een groep") but that doesn't seem to work, nor does @Html.DropDownListFor( ViewBag.model.list,"Selecteer een groep")
Found it: @Html.DropDownListFor(model => model.selectedItem, ViewBag.model.list as IEnumerable<SelectListItem>,"Selecteer een groep")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.