Im using a ModelView in order to create a DropDownList:
ModelView
public class CreateSubjectModel
{
public Subject Subject { get; set; }
public IEnumerable<SelectListItem> Careers { get; set; }
}
The Action [HttpGet]
public ActionResult Create()
{
CreateSubjectModel model = new CreateSubjectModel();
List<SelectListItem> careerList = new List<SelectListItem>();
foreach (var career in _careerManager.GetCareers())
careerList.Add(new SelectListItem() { Text = career.Institution + " - " + career.Name, Value = career.id.ToString() });
model.Careers = careerList;
return View(model);
}
The Action [HttpPost]
public ActionResult Create(CreateSubjectModel createSubjectModel)
{
try
{
_subjectManager.SaveSubject(createSubjectModel.Subject);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And the View:
<div class="editor-field">
@Html.DropDownListFor(model => model.Careers, Model.Careers)
@Html.ValidationMessageFor(model => model.Careers)
</div>
The problem is that when I hit Submit I have this problem:
**Object reference not set to an instance of an object.**
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 47: </div>
Line 48: <div class="editor-field">
Line 49: @Html.DropDownListFor(model => model.Careers, Model.Careers)
Line 50: @Html.ValidationMessageFor(model => model.Careers)
Line 51: </div>
I look everywhere and i cant find a working example or some tutorial that helps me. Hope you can do...