Daven Patel
Daven Patel

Reputation: 176

ASP .NET MVC Custom Profile Settings Not Found Issue

Currently I am using a custom profile in my ASP .NET MVC application. When creating users and setting the profile properties everything works fine. The issue I am having is that when I edit a user I keep on getting a settings property 'FirstName' was not found error.

Here is the code I am using, and from my understanding it should work fine:

User Profile Class

public class UserProfile : ProfileBase
{
    [SettingsAllowAnonymous(false)]
    public string FirstName
    {
        get { return base["FirstName"] as string; }
        set { base["FirstName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string LastName
    {
        get { return base["LastName"] as string; }
        set { base["LastName"] = value; }
    }

    public static UserProfile GetUserProfile()
    {
        return Create(Membership.GetUser().UserName) as UserProfile;
    }

    public static UserProfile GetUserProfile(string username)
    {
        return Create(username) as UserProfile;
    }
}

Web.config

<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

<profile inherits="MissionManager.Models.UserProfile">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>

Calling Code - Note that before I even hit this method, ASP .NET is running into the error mentioned above.

   [HttpPost]
    public ActionResult Edit(UserModel model)
    {
        try
        {
            MembershipService.UpdateUser(User.Identity.Name, model);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

Upvotes: 1

Views: 559

Answers (1)

Peter Mourfield
Peter Mourfield

Reputation: 1885

I have something similar that works. The differences are:

public string FirstName
{
    get { return this["FirstName"] as string; }
    set { this["FirstName"] = value; }
}

public string LastName
{
    get { return this["LastName"] as string; }
    set { this["LastName"] = value; }
}

I don't have the attributes on the custom properties and I use this instread of base

HTH

Upvotes: 1

Related Questions