Domanda

enter code hereI have read a few other posts like mine, but still no answers that are available.

I have an editorFor my profile information, and it gets the data fine for the selected user, but once I try and save it, I get the following error.

The settings property 'FirstName' was not found.

The thing is for the POST, operation Im not even doing anything yet. I think its trying to load, or get a profile object to fill the model once posted. Theres nothing in the callstack from what I can see to find where this is happening.

Web.Config:

 <profile inherits="MyWeb.BusinessLayer.Models.Account.Profile" enabled="true" defaultProvider="AspNetSqlProfileProvider">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="My Website"/>
  </providers>
</profile>

Profile Class

public class Profile : ProfileBase
{
    [Display(Name = "First Name")]
    [Required(ErrorMessageResourceName = "err_req_field", ErrorMessageResourceType = typeof(Resources))]
    [StringLength(50, MinimumLength = 3, ErrorMessageResourceName = "err_len_field", ErrorMessageResourceType = typeof(Resources))]
    public virtual string FirstName
    {
        get
        {
            return (this.GetPropertyValue("FirstName").ToString());
        }
        set
        {
            this.SetPropertyValue("FirstName", value);
        }
    } ... abreviated

Controller Actions

    public ViewResult EditProfile(Guid id)
    {
        var user = _userService.Get(id);

        Profile _profile = Profile.GetProfile(user.UserName);

        return View(_profile);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public RedirectToRouteResult EditProfile(Profile profile)
    {
        return RedirectToAction("Index" , "UserAdministration" );
    }

The Display Editor works fine, I get the data, its just when I try the Post, that I get this. Any other ideas? My only other idea, might be to do an Ajax Post, with the paramerts split out rather than the Profile model, then just read and set./.. Thoughts?

È stato utile?

Soluzione

After you post your form with Profile class the model binder tries to bind the model so it wants all the properties ('FirstName' among others).

The problem is that the model from the form is not complete (and it can't be) so the Profile is acting as not authorized and not known.

You can see it when you set breakpoint before the getter for FirstName property. After the breakpoint hits, look to Locals window and locate 'this'. Expand its 'base' property and you will see that the IsAnonymous property is set to true.

The binder cannot recognize the profile after it comes from POST form. The solution is to code it like this:

[AcceptVerbs(HttpVerbs.Post)]
public RedirectToRouteResult EditProfile(ProfileModel profileModel)
{
    // get the current user profile
    Profile profile = Profile.GetUserProfile();
    profile.FirstName = profileModel.FirstName;

    // save the profile
    profile.save();

    return RedirectToAction("Index" , "UserAdministration" );
}

So you create user profile based on current logged user and then update some properties.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top