I am trying to create a drop down list using asp.net mvc.
Model:
public string Status { get; set; }
public List<SelectListItem> StatusList { get; set; }
public AddUser()
{
StatusList = new List<SelectListItem>
{
new SelectListItem{Value = "0",Text = "0"},
new SelectListItem{Value = "1",Text = "1"}
};
}
View:
<%: Html.DropDownListFor(m=>m.Status,new SelectList(Model.StatusList,"Value","Text")) %>
<%: Html.ValidationMessageFor(m => m.Status) %>
I don’t know why but I keep getting this error:
Object reference not set to an instance of an object.
Anyone knows what I am doing wrong?
This error usually happens when you leave a field of model null or the model is null
Make sure that “Status” isn’t null and your return the model in “View” just like this:
And one more tip for u:
The class
SelectListimplementsIEnumerable<SelectListItem>so in view your code may look something like this:You don’t need instantiate again.