Solved

Question on MVC

Posted on 2013-12-17
3
259 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

12 Experts available now in Live!

Get 1:1 Help Now