Solved

Question on MVC

Posted on 2013-12-17
3
257 Views
Last Modified: 2013-12-17
Hi Experts,

We have MVC4 Application and am going through the code and found following statement in index.cshtml file to get login ID.

var loginID='@((User as Sample.Project.Principal.Administrator).Name);

Open in new window

1)can anyone briefly explain User as in  the above statement

2)How can I write above statement without using "User as "

Thanks in Advance
0
Comment
Question by:ksd123
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
ID: 39723788
1) That line casts the User variable/property to a Sample.Project.Principal.Administrator object and then gets the value of its Name property.

2) That depends on the native type of User and if it natively supports a Name property.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 39724836
To add to carl_tawn's #1, there is an (possibly valid) assumption here that User *is* or *descends from* the Sample.Project.Principal.Administrator class. If it does not, then the as operator will return null, and the access of the Name property will throw a NullReferenceException. The as operator will successfully cast if the type is compatible, or it will return null. Generally speaking, you should be checking for the null return when using as.

See:  http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
0
 

Author Closing Comment

by:ksd123
ID: 39725903
Thank you guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now