albert albert

albert albert

  • NA
  • 524
  • 0

Dropdownlist

Jul 19 2014 12:20 PM
Hi everybody.

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<FaqCategorie>Categorie;
    public int SelectedCategoriedFaqId { get; set; }     public IEnumerable<SelectListItem> 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";
}
<h2>Support</h2> @using (Html.BeginForm())
{
//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)  @*}*@
[/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:

<span class="fixedLabelWidth">@Html.LabelFor(model => model.SelectedCategoriedFaqId, "Categorie:")</span>
    @Html.DropDownListFor(x => x.SelectedCategoriedFaqId, Model.FAQCategorieItems ) 
Thank you