I have this:
Controller:
[code]
public ActionResult Index()
{ //var cats = faqCategorieService.GetAll().ToList(); var categories = faqCategorieService.GetAll().OrderBy(x => x.Naam) .Select(a => new FaqOverviewModel() { Id = a.Id, Naam = a.Naam }).ToList(); foreach (var categorie in categories) { categorie.Naam = categorie.Naam + " (" + categorie.Id.ToString() + ")"; } categories.Insert(0, new FaqOverviewModel() { Id = -1, Naam = "Maak een keuze..." }); return View(); } [/code] ModelView:
[code]
private readonly List
public int SelectedCategoriedFaqId { get; set; } public IEnumerable FAQCategorieItems { get { return new SelectList(Categorie, "Id", "Naam"); } } [/code] View:
[code]
@using Multitask.Regenboog.Medicijnverstrekking.Domain
@model Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel
@{
ViewBag.Title = "Index";}Support
@using (Html.BeginForm()){
//if (Model.ZorgverzekeraarInvoerEnabled) //{ @Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:") @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems ) //@Html.HiddenFor(model => model.ZorgverzekeraarInvoerEnabled) @* @Html.LabelFor(m => m) *@ @* @Html.LabelFor(model => model.SelectedSubCategorieFaqId, "Onderwerp:") @Html.DropDownListFor(m => m.SelectedSubCategorieFaqId, Model.FaqSubCategorieItems, new { style = "width: 150px" }) *@ @Html.LabelFor(model => model.EmailBericht, "Bericht:") @Html.TextAreaFor(x => x.EmailBericht) @*}*@[/code] Now if I loop through the Index from the Controller I see nice the data
But in the view I get this error:
An exception of type 'System.NullReferenceException' occurred in App_Web_qdasymuj.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
By this dropdownListFor:
>@Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:")
@Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems ) Thank you
Replies
Know the answer? Post it — somebody with the same question will find it here.