Finally giving up on asking the Google Gods...
I'm trying to access ViewBag variables in my view from the query below.
using (var context = new PrincipalContext(ContextType.Domain))
{
var un = UserPrincipal.FindByIdentity(context, User.Identity.Name);
var userinfo = db.Users
.Where(x => x.sAMAccountName == un.SamAccountName)
.Select(x => new
{
x.sAMAccountName,
x.Id
}).First();
ViewBag.UserInfo = userinfo;
}
I see the variables in VS' breakpoint watcher
However, when I try to access them:
Not 100% certain on how I'm supposed to be accessing these? I don't want to iterate through them because they're getting placed in a form as fields. As previously mentioned, the Google Gods were less than helpful. Thanks!
UPDATE
I took a closer look based on the responses received, regarding casting. Ultimately what I ended up doing was retrieving the entire model and returning that in the ViewBag, and accessing it as such:
using (var context = new PrincipalContext(ContextType.Domain))
{
var un = UserPrincipal.FindByIdentity(context, User.Identity.Name);
User info = new User();
info = (User)db.Users
.Where(x => x.sAMAccountName == un.SamAccountName)
.First();
ViewBag.User = info;
}
And then in the view
@ViewBag.User.Id
@ViewBag.User.sAMAccountName
Ultimately I was looking to access these variables without iterating over them in a loop. When I add a select statement into my query, it begins to complain that I'm trying to cast anonymous strings to a model object. Is one able to retrieve select data from a model in ASP, or do you need to return an entire model set? Now it's just curiosity. Thank you for the guidance!