自定义配置文件提供程序:找不到设置属性'' - MVC3

时间:2012-05-25 09:26:56

标签: c# asp.net-mvc asp.net-mvc-3

尝试编辑/更新用户个人资料时,我遇到了这个问题。基本上当用户注册时,他们将bool调用'AuthorizedTeamMember'设置为false。然后,他们的团队经理可以将其设置为true以授权用户。

但是,当点击“授权”按钮授权用户时,我收到以下错误:

The settings property 'AuthorizedTeamMember' was not found.

我已经好好了解一下找到解决方案,但似乎没有任何效果。在点击HttpPost上的'AuthorizePlayer'方法之前,它似乎是错误的。

无论如何,这是我的自定义配置文件提供商:

namespace iConfirm.Models
{
    public class TeamPlayer : ProfileBase
    {
        [Display(Name = "First Name")]
        public virtual string FirstName
        {
            get
            {
                return (this.GetPropertyValue("FirstName").ToString());
            }
            set
            {
                this.SetPropertyValue("FirstName", value);
            }
        }

        [Display(Name = "Last Name")]
        public virtual string LastName
        {
            get
            {
                return (this.GetPropertyValue("LastName").ToString());
            }
            set
            {
                this.SetPropertyValue("LastName", value);
            }
        }
        [Display(Name = "Date Of Birth")]
        public virtual string DateOfBirth 
        {
            get
            {
                return (this.GetPropertyValue("DateOfBirth").ToString());
            }
            set
            {
                this.SetPropertyValue("DateOfBirth", value);
            }
        }
        [Display(Name = "Phone Number")]
        public virtual string PhoneNumber
        {
            get
            {
                return (this.GetPropertyValue("PhoneNumber").ToString());
            }
            set
            {
                this.SetPropertyValue("PhoneNumber", value);
            }
        }
        public virtual string TeamId
        {
            get
            {
                return (this.GetPropertyValue("TeamId").ToString());
            }
            set
            {
                this.SetPropertyValue("TeamId", value);
            }
        }
        public virtual bool AuthorizedTeamMember
        {
            get
            {
                return (bool)base["AuthorizedTeamMember"]; //<-- Errors here on post
            }
            set
            {
                base["AuthorizedTeamMember"] = value;
            }
        }
        public static TeamPlayer GetProfile(string username)
        {
            return Create(username) as TeamPlayer;
        }

        public static List<TeamPlayer> GetProfilesByTeam(int teamId)
        {
            //var profileList = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
            var profilesInTeam = Membership.GetAllUsers().Cast<MembershipUser>()
            .Select(user => TeamPlayer.Create(user.UserName, true) as TeamPlayer)
            .Where(profile => profile.GetPropertyValue("TeamId").ToString().Equals(teamId.ToString()))
            .ToList();

            return profilesInTeam;

        }
    }
}

以下是授权用户的代码:

        [HttpPost]
        public ActionResult AuthorizePlayer(TeamPlayer teamPlayer)
        {
            teamPlayer.AuthorizedTeamMember = true;
            var user = TeamPlayer.GetProfile(teamPlayer.UserName);
                user.FirstName = teamPlayer.FirstName; 
                user.LastName = teamPlayer.LastName;
                user.AuthorizedTeamMember = teamPlayer.AuthorizedTeamMember;
                user.DateOfBirth = teamPlayer.DateOfBirth;
                user.PhoneNumber = teamPlayer.PhoneNumber;
                user.Save();
            /*TeamPlayer _teamPlayer = TeamPlayer.Create(teamPlayer.UserName) as TeamPlayer;
            //var tempProfile = TeamPlayer.Create(teamPlayer.UserName) as TeamPlayer;
            _teamPlayer.AuthorizedTeamMember = true;
            _teamPlayer.Save();
            //Email user that they have been authorized*/
            return View("Index");
        }

最后是观点:

@model iConfirm.Models.TeamPlayer
@{
    ViewBag.Title = "AuthorizePlayer";
    MembershipUser user = Membership.GetUser(Model.UserName);
}

<h2>AuthorizePlayer</h2>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Authorize the following user?</legend>
        @Html.HiddenFor(model => model.AuthorizedTeamMember)
        <div class="display-label">
            Username
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.UserName)
        </div>
        <div class="display-label">
            Full Name
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)
        </div>
        <div class="display-label">
            Email
        </div>
        <div class="display-field">
            @user.Email
        </div>
        <div class="display-label">
            Telephone number
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.PhoneNumber)
        </div>
        <div class="display-label">
            Date of Birth
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.DateOfBirth)
        </div>

        <p>
            <input type="submit" value="Authorize" />
        </p>
    </fieldset>
}

非常感谢任何帮助。

亚历

1 个答案:

答案 0 :(得分:0)

可能会遗漏一些东西但是你有使用

的原因

this.SetPropertyValue()

除了base [“AuthorizedTeamMember”] = value;

之外的所有属性