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
Post a Comment