Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In ASP MVC you don't have a profile object generated for your site with the properties defined in the web.config file. The property <code>Profile</code> that can be accessed from the controller methods is of type <code>ProfileBase</code> (<a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.profile%28v=vs.108%29.aspx" rel="nofollow noreferrer">see msdn</a>) and does not contains strongly typed properties for your custom profile properties. As you may also be aware, this profile is loaded for the logged in user at the beggining of the request and any changes are saved at the end of the request.</p> <p>There are <a href="http://www.codeguru.com/csharp/.net/net_asp/article.php/c19797/Dealing-with-Profile-Access-in-ASPNET-MVC-Applications.htm" rel="nofollow noreferrer">different ways</a> you can work the <code>ProfileBase</code> class. The most used ones are:</p> <ul> <li>use directly the ProfileBase class </li> <li>create a derived custom profile class.</li> </ul> <p>When <code>ProfileBase</code> is used directly, you need to either use the instance from the controller method or get an instance given an user name. Then you should use indexers to access the profile properties. Let's say your controller method received an object of type UserModel that contains your user data like an email and the BadgerName, then you could write code like this:</p> <pre><code>//Getting the instance from the controller property: ProfileBase profile = this.Profile; //or even: this.HttpContext.Profile //You can also get the profile for a given existing user. ProfileBase profile = ProfileBase.Create(userModel.Name); //Then update properties using indexer profile["Email"] = userModel.Email; profile["BadgerName"] = userModel.BadgerName; //Manually save changes //(Can be skipped for the profile automatically loaded in the Controller) profile.Save(); </code></pre> <p>However if you create a derived class from <code>ProfileBase</code>, you will end up using your class much in the same way you originally intended. You will basically create a wrapper class with strongly typed properties that internally access the ProfileBase using indexers (A summary of the approach is <a href="https://stackoverflow.com/questions/8305233/asp-net-mvc-3-user-profiles-not-being-generated">here</a>):</p> <pre><code>public class MyCustomProfile : ProfileBase { public string Email { get { return base["Email"] as string; } set { base["Email"] = value; } } public string BadgerName { get { return base["BadgerName"] as string; } set { base["BadgerName"] = value; } } //If needed, you can provide methods to recover profiles //for the logged in user or any user given its user name public static MyCustomProfile GetCurrent() { return Create(Membership.GetUser().UserName) as MyCustomProfile; } public static MyCustomProfile GetProfile(string userName) { return Create(userName) as MyCustomProfile; } } </code></pre> <p>If you use this option, you also need to make sure the <code>&lt;profile&gt;</code> element of the web.config has the <code>inherits</code> attribute set to your custom model class:</p> <pre><code>&lt;profile enabled="true" defaultProvider="DefaultProfileProvider" inherits="yourNamespace.MyCustomProfile"&gt; </code></pre> <p>With this code and config in place, you can start using your custom profile class by either recovering user profiles yourselve or by casting the controller Profile property to your custom class: </p> <pre><code>//Cast the Profile property of the controller to your custom class MyCustomProfile profile = this.Profile as MyCustomProfile // or even: HttpContext.Profile as MyCustomProfile //You could also manually load the profile for given an user name profile = MyCustomProfile.GetProfile(userModel.Name); //Or even manually load the profile for the logged in user profile = MyCustomProfile.GetCurrent(); //Now get/set the profile properties using the strongly typed properties of your class profile.Email= userModel.Email; profile.BadgerName= userModel.BadgerName; //Manually save changes //(Can be skipped for the profile automatically loaded in the Controller) profile.Save(); </code></pre> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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