Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you get the Profile variable populated in a view in ASP.NET MVC?
    text
    copied!<p>In a view in ASP.NET MVC I can access the profile and profile members like this:</p> <pre><code>&lt;%= Profile.Name %&gt; - &lt;%= Profile.Karma %&gt; </code></pre> <p>but how do I get the Profile variable populated? It seems to be in HttpContext.Current.Profile. I've wrote a custom Profile class with a CurrentUser method that looks like this:</p> <pre><code>static public Profile CurrentUser { get { return (Profile) (ProfileBase.Create(Membership.GetUser().UserName)); } } </code></pre> <p>I have that class pointed to in my Web.config:</p> <pre><code>&lt;profile inherits="MyProject.Models.Profile" defaultProvider="AspNetSqlProfileProvider" enabled="true"&gt; &lt;providers&gt; &lt;clear /&gt; &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" /&gt; &lt;/providers&gt; &lt;/profile&gt; </code></pre> <p>This:</p> <pre><code>var p = CurrentUser.Profile </code></pre> <p>successfully gives me the profile, but I cannot set it like this:</p> <pre><code>HttpContext.Profile = CurrentUser.Profile; </code></pre> <p>because HttpContext.Profile is read only. I get the error:</p> <blockquote> <p>Error 18 Property or indexer 'System.Web.HttpContextBase.Profile' cannot be assigned to -- it is read only C:...\Controller.cs 26 17 MyProject</p> </blockquote> <p>Also, I need the Profile to be assigned for every view, so that the name of the logged in user can be displayed on the top right in the usual way. How should I do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload