0

In my application I have the following message: Object reference not set to an instance of an object.

@Html.DropDownList("Title",ViewData["PersonTitle"] as SelectList, Model.Person.Title)

The dropdownlist contains simple strings (person titles). When you select a title, that title should be returned to my model. Controller code:

ViewData["PersonTitle"] = new SelectList(new[] { "Dhr.", "Mevr." });

Why is this not working ?

Edit: The purpose is to change

@Html.EditorFor(model => model.Person.Title)

into something like this

@Html.DropDownList("Title",ViewData["PersonTitle"] as SelectList, Model.Person.Title)
Aliostad
  • 80,612
  • 21
  • 160
  • 208
Thomas
  • 297
  • 1
  • 8
  • 21
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – tom redfern Jan 11 '17 at 10:29

2 Answers2

3

Model.Person is null. You need to find out why this is not getting populated. The most common reason would be not outputing to the form so it does not get posted back.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • ok, but i need to replace this part "@Html.EditorFor(model => model.Person.Type)" by a dropdownlist. And the value of that dropdown should be send to the same place as the editor would do. (Model.Person.Type) – Thomas Mar 29 '11 at 09:03
  • Instead of `"Title"` it needs to be `"Person.Title"` – Aliostad Mar 29 '11 at 09:11
  • the "Title" after dropdownlist ? that is just the name of dropdownlist. For avoiding misunderstanding, it is model.person.title and NOT model.Person.Type (could not change my comment anymore). If i use the editorfor(model=> model.Person.Type) , it send the value of what i wrote to the model – Thomas Mar 29 '11 at 09:23
  • found it : had to use @html.dropdownlistfor – Thomas Mar 29 '11 at 10:49
  • Yes, using generic extensions are 100 times better :) I did not recommend it to you since you were using ViewData but now it is good you have changed it. – Aliostad Mar 29 '11 at 10:50
0

i had to use @html.dropdownlistfor

Thomas
  • 297
  • 1
  • 8
  • 21