Getting error: The name 'Profile' does not exist in the current context in razor view

Posted by Santosh.Impossible under ASP.NET MVC on 7/3/2014 | Points: 10 | Views : 1584 | Status : [Member] | Replies : 1
I am trying to access Profile object properties in the razor view (asp.net mvc 3).
I have set the following the web.config file:

<profile defaultProvider="DemoProfileProvider" enabled="true" inherits="Demo.Common.Web.TestProfile">
<providers>
<clear />
<add name="DemoProfileProvider" type="Demo.Common.Web.TestProfile" />
</providers>
<properties>
<add name="FirstName" />
<add name="LastName" />
<add name="UserType" />
</properties>
</profile>

public class ProfileCommon : TestProfile
{
public ProfileCommon();

public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string UserType { get; set; }

public virtual ProfileCommon GetProfile(string username);
}


I am setting the profile properties in the Global.asax.cs file:
void WSFederationAuthenticationModule_SignedIn(object sender, EventArgs e)
{
var profile = TestProfile.GetProfile(HttpContext.Current.User.Identity.Name);
if (profile.ProfileGuid == Guid.Empty)
profile.ProfileGuid = Guid.NewGuid();
if (string.IsNullOrEmpty(profile.Language))
profile.Language = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
//Set FirstName and LastName,UserType in Profile
if(((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Exists(c => c.ClaimType == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"))
profile.SetPropertyValue("FirstName", ((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Single(c => c.ClaimType == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value);
if(((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Exists(c => c.ClaimType == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"))
profile.SetPropertyValue("LastName", ((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Single(c => c.ClaimType == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value);
if (((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Exists(c => c.ClaimType == "http://schemas.deloitte.com/ws/2011/05/identity/claims/usertype"))
profile.SetPropertyValue("UserType", ((IClaimsPrincipal)Thread.CurrentPrincipal).Identities[0].Claims.Single(c => c.ClaimType == "http://schemas.deloitte.com/ws/2011/05/identity/claims/usertype").Value);
profile.Save();

}



Now I am trying to access Profile properties like FirstName and LastName in the _Layout.cshtml but it throwing an error: The name 'Profile' does not exist in the current context
@if (Request.IsAuthenticated)
{
<br />
if ((!string.IsNullOrWhiteSpace(Profile.FirstName) && (!string.IsNullOrWhiteSpace(Profile.LastName))))
{
<b>@Profile.FirstName + " " + @Profile.LastName</b>
}
else
{
<b>@Html.ViewContext.HttpContext.User.Identity.Name</b>
}

}


Can any one help me to resolve the above issue?

santosh kumar patro


Responses

Posted by: Vuyiswamb on: 7/6/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Please upgrade to MVC 4 , it is a known issue you are having

Thank you for posting at Dotnetfunda
[Administrator]

Santosh.Impossible, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response