0

When i put a break point on Index method and after executing the line _model.SubCategoryList.Add(new SelectListItem { Text = "Select Any ", Value = "0" });, I'm getting below error.

An exception of type 'System.NullReferenceException' occurred in Proj.Web.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

public ActionResult Index()
    {
        _model = new FA_AssetCreationModel { };            
        _model.SubCategoryList.Add(new SelectListItem { Text = "Select Any ", Value = "0" });           
        return View("Creation", _model);
    }

I need to set a default value for the dropdownlist on page load inorder to avoid another error Value cannot be null. Parameter name: items.

Limna
  • 401
  • 10
  • 28
  • Because `SubCategoryList` is `null` - you have not initialized it –  Nov 05 '17 at 04:07
  • Can you help me to initialize `Text = "Select Any ", Value = "0"` to the list `public List SubCategoryList { get; set; }` – Limna Nov 05 '17 at 04:14
  • 1
    `_model = new FA_AssetCreationModel { SubCategoryList = new List() };` - now its not `null` so you can add items to it –  Nov 05 '17 at 04:44
  • 1
    Alternatively, initialize it in the default constructor for `FA_AssetCreationModel` - `public FA_AssetCreationModel() { SubCategoryList = new List(); }` –  Nov 05 '17 at 04:45

0 Answers0