I have this:
Controller:
[code]
public ActionResult Index()
Code
{ //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(); }ModelView:
[code]
private readonly ListCategorie;
C#
public int SelectedCategoriedFaqId { get; set; } public IEnumerable<SelectListItem> FAQCategorieItems { get { return new SelectList(Categorie, "Id", "Naam"); } }View:
[code]
@using Multitask.Regenboog.Medicijnverstrekking.Domain
@model Multitask.Regenboog.Medicijnverstrekking.WebApplication.ViewModels.FAQ.FaqOverviewModel
@{
Code
ViewBag.Title = "Index";Support
@using (Html.BeginForm()){
HTML / XML
//if (Model.ZorgverzekeraarInvoerEnabled) //{ <span class="fixedLabelWidth">@Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:")</span> @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems ) //@Html.HiddenFor(model => model.ZorgverzekeraarInvoerEnabled) @*<div class="editor-label"> @Html.LabelFor(m => m) </div>*@ @*<div class="editor-field"> <span class="fixedLabelWidth">@Html.LabelFor(model => model.SelectedSubCategorieFaqId, "Onderwerp:")</span> @Html.DropDownListFor(m => m.SelectedSubCategorieFaqId, Model.FaqSubCategorieItems, new { style = "width: 150px" }) </div>*@ <span class="fixedLabelWidth">@Html.LabelFor(model => model.EmailBericht, "Bericht:")</span> @Html.TextAreaFor(x => x.EmailBericht) @*}*@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:")
Code
@Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems )
Answers
Know the answer? Post it. Somebody with the same question will find it here.