I am getting error of "system.nullreferenceexception object reference not set to an instance of an object" while storing ID of dropdown list. I have gone through many sites and posts but still I am stuck in this error. Please help me to solve this error.
Model:
[Required]
[Display(Name = "Profession")]
public virtual string professionid { get; set; }
IEnumerable<SelectListItem> professionList = new List<SelectListItem>();
public SelectList getProfession()
{
professionList = (from m in _db.ProfessionInfos select m).AsEnumerable().Select(m => new SelectListItem() { Text = m.P_Name, Value = m.P_id.ToString() });
return new SelectList(professionList, "Value", "Text", professionid);
}
Controller:
[HttpGet]
public ActionResult Registration()
{
var model = new M_Reg();
return View(model);
}
View:
<div class="editor-label">
@Html.LabelFor(m => m.professionid)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.professionid,Model.getProfession(),"Choose Profession")
@Html.ValidationMessageFor(m => m.professionid)
</div>