Solved

Question on MVC

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 40
Vb.net threads keep increasing 2 38
Google Maps with Webforms 1 14
TFS 2015 Access denied. 1 16
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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