I am probably doing something silly but cannot find out what. I am trying to modify simple membership functionality in ASP.NET MVC 4. I have slightly modified a RegisterModel coming with the template, and added a list of categories to it like so:
public class RegisterModel
{
...
public List<SelectListItem> Categories { get; set; }
}
Then in the account controller I’m trying to add an item to this list but get “Object reference not set to an instance of an object.” error:
[AllowAnonymous]
public ActionResult Register()
{
RegisterModel rm = new RegisterModel();
//SelectListItem guestCategory = new SelectListItem();
//guestCategory.Value = null;
//guestCategory.Text = "Guest";
rm.Categories.Add(new SelectListItem { Value = null, Text = "Guest" });
...
Any ideas why?
you just need to do this before adding items to list , because when you add items its not get instantiated that why its giving error
that means in this method do like this
or
you can do same thing in constructor of
RegisterModel.