I am using this example to develop a dropdownlist. It works well until I make some changes in the way my model is called within my view. The dropdownlist model class is called dropdownModel. Because my view contains 2 models, I created a "big" model class, BigModelClass that holds my two models.
The big model looks like this
public class BigModelClass {
public DropDownModel dropDownModel { get; set; }
public IEnumerable<projectname.Model.model2> var2 { get; set; }
}
In my view, I call the model as:
@model BigModel
Now in my view, I call use the dropdownlist as follows:
@Html.LabelFor(m => m.dropDownModel.State)
@Html.DropDownListFor(m => m.dropDownModel.State,
new SelectList(Model.dropDownModel.StateList, "Value", "Text"))
<span class="required"></span>
@Html.ValidationMessageFor(m => m.dropDownModel.State)
Unfortunately, I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
on the line
@Html.DropDownListFor(m => m.dropDownModel.State, new SelectList(Model.dropDownModel.StateList, "Value", "Text"))
Averything works fine if I use only the dropDownModel model.
Any help is very appreciated
EDIT The controller for the view:
public ActionResult Index(){
return View (new BigModelClass());
}