c# - Filling ViewBag from database returns NullReferenceException -


i'm working asp.net mvc 4 , i'm building registering form new members. each member has role (administrator or simple user). i'm trying populate dropdownlist database , i'm doing in controller :

        [httpget]         public actionresult test()         {             viewbag.roles = context.roles.tolist();              return view();         } 

however, i'm getting nullreferenceexception error , don't see why. roles table has records don't know why returning null exception.

if of code, it's context not initialized. want like:

[httpget] public actionresult test() {     using (var context = new mydatacontext())     {         viewbag.roles = context.roles.tolist();         return view();     } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -