配置文件对象 +查看模型 +更新用户配置文件 MVC C#

发布于 2024-10-19 04:12:27 字数 7637 浏览 1 评论 0原文

问题: 我已经创建了一个自定义配置文件对象,如 Joel 此处所述。然后我使用了 Jeremy 的方法(此处)扩展自定义配置文件,以允许我使用生成用户并设置这些值。然后,我创建了一个 ViewModel 来显示 Memeberhip 信息和个人资料信息,以便用户可以更新其会员信息(电子邮件)和个人资料信息。 **视图显示了我在视图中更新的信息中输入的所有字段,然后单击“保存”,其中出现以下错误

System.Configuration.SettingsPropertyNotFoundException:未找到设置属性“FirstName”。 **

这是我的自定义配置文件对象(模型):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using System.Web.Security;


namespace NDAC.Models
{
    public class ProfileInformation : ProfileBase
    {
        static public ProfileInformation CurrentUser
        {
            get
            {
                if (Membership.GetUser() != null)
                {
                     return (ProfileInformation)(ProfileBase.Create(Membership.GetUser().UserName));
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual string FirstName {
            get
            {

                return ((string)(base["FirstName"]));
            }
            set 
            {
                base["FirstName"] = value; Save();
            } 
        }
        public virtual string LastName {
            get 
            {
                return ((string)(base["LastName"]));
            }
            set 
            {
                base["LastName"] = value; Save();
            }
        }
        public string Street 
        {
            get 
            {
                return ((string)(base["Street"]));
            }
            set 
            {
                base["Street"] = value; Save();
            }
        }
        public string Street2 
        { 
            get 
            {
                return ((string)(base["Street2"]));
            }
            set 
            {
                base["Street2"] = value; Save();
            }
        }
        public string City 
        {
            get 
            {
                return ((string)(base["City"]));
            }
            set 
            {
                base["City"] = value; Save();
            }
        }
        public string State 
        {
            get 
            {
                return ((string)(base["State"]));
            }
            set 
            {
                base["State"] = value; Save();
            }
        }
        public string ZipCode 
        {
            get 
            {
                return ((string)(base["ZipCode"]));
            }
            set 
            {
                base["ZipCode"] = value; Save();
            }
        }
        public string PhoneNumber 
        {
            get 
            {
                return ((string)(base["PhoneNumber"]));
            }
            set 
            {
                base["PhoneNumber"] = value; Save();
            }
        }
        public string SemesterID 
        {
            get 
            {
                return ((string)(base["SemesterID"]));
            }
            set 
            {
                base["SemesterID"] = value; Save();
            }
        }

        static public  ProfileInformation GetProfile(string username)
        {
            return Create(username) as ProfileInformation;
        }

        internal static ProfileInformation NewUser
        {
            get { return System.Web.HttpContext.Current.Profile as ProfileInformation; }
        }

    }
}

这是 UserProfile 获取方法:

[Authorize]
        public ActionResult UserProfile()
        {
            string id = User.Identity.Name.ToString();
            MembershipUser user = Membership.GetUser(id);

           var model = new UserViewModel();

            UserInformation userInf = new UserInformation();
            userInf.Username = user.UserName;
            userInf.Email = user.Email;

            ProfileInformation currentProfile = ProfileInformation.GetProfile(user.UserName);

            model.Profile = currentProfile;
            model.UserInf = userInf;    

            return View(model);

        }

如您所见,我正在使用视图模型来显示视图。 视图模型是这样的:

 public class UserViewModel
    {

        public UserInformation UserInf{ get; set; }
        public ProfileInformation Profile { get; set; }


    }

最后更新 UserProfile 的 HttpPost 方法是:

[Authorize]
        [HttpPost]
        public ActionResult UserProfile(UserViewModel model)
        {
            ProfileInformation currentProfile = ProfileInformation.GetProfile(User.Identity.Name.ToString());

            ProfileInformation.CurrentUser.FirstName = model.Profile.FirstName;
            currentProfile.LastName = model.Profile.LastName;
            currentProfile.Street = model.Profile.Street;
            currentProfile.Street2 = model.Profile.Street2;
            currentProfile.City = model.Profile.City;
            currentProfile.State= model.Profile.State;
            currentProfile.ZipCode = model.Profile.ZipCode;
            currentProfile.PhoneNumber = model.Profile.PhoneNumber;
            currentProfile.Save();

            MembershipUser user = Membership.GetUser(User.Identity.Name.ToString());
            user.Email = model.UserInf.Email;
            Membership.UpdateUser(user);


            TempData["SuccessMessage"] = "Your Profile information has been saved";
            ViewBag.Title = "My Profile";
            return RedirectToAction("ProfileUpdated");
        }

问题是我无法弄清楚错误来自哪里。该表单呈现我输入的有效信息,然后我被告知“未找到设置属性‘FirstName’”。这就像 UserViewModel 试图更新 ProfileInformation.cs (自定义配置文件对象),但它说它找不到该对象...我想这是因为它没有要更新的配置文件...但我在发布之前无法找到错误...太令人沮丧了!任何帮助将不胜感激!感谢您花时间查看此内容。

我还将为您留下我的 Register 方法,该方法成功使用自定义 Profile 对象,甚至将 name 属性更新为“ScoobyDoo”以用于测试目的:)

[HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretAnswer);

                if (createStatus == MembershipCreateStatus.Success)
                {

                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                    Roles.AddUserToRole(model.UserName, "Student");
                    ProfileInformation.NewUser.Initialize(model.UserName, true);
                    ProfileInformation.NewUser.Save();

                    string currentSemesterID = CurrentSemester;
                    ProfileInformation profile = ProfileInformation.GetProfile(model.UserName);
                    profile.SemesterID = currentSemesterID;
                    profile.FirstName = "ScoobyDoo";

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

Problem:
I have created a custom profile object as described by Joel here. I then used Jeremy's method (here) to extend the custom profile to allow me to use generate a user and set those values. I then created a ViewModel to display Memeberhip information and Profile Information so that the user can update their membership info (email) and Profile Information. **The view displays all of the fields I enter in the updated information in the view and click save, where I get the following error

System.Configuration.SettingsPropertyNotFoundException: The settings property 'FirstName' was not found. **

Here's my custom Profile Object (Model):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using System.Web.Security;


namespace NDAC.Models
{
    public class ProfileInformation : ProfileBase
    {
        static public ProfileInformation CurrentUser
        {
            get
            {
                if (Membership.GetUser() != null)
                {
                     return (ProfileInformation)(ProfileBase.Create(Membership.GetUser().UserName));
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual string FirstName {
            get
            {

                return ((string)(base["FirstName"]));
            }
            set 
            {
                base["FirstName"] = value; Save();
            } 
        }
        public virtual string LastName {
            get 
            {
                return ((string)(base["LastName"]));
            }
            set 
            {
                base["LastName"] = value; Save();
            }
        }
        public string Street 
        {
            get 
            {
                return ((string)(base["Street"]));
            }
            set 
            {
                base["Street"] = value; Save();
            }
        }
        public string Street2 
        { 
            get 
            {
                return ((string)(base["Street2"]));
            }
            set 
            {
                base["Street2"] = value; Save();
            }
        }
        public string City 
        {
            get 
            {
                return ((string)(base["City"]));
            }
            set 
            {
                base["City"] = value; Save();
            }
        }
        public string State 
        {
            get 
            {
                return ((string)(base["State"]));
            }
            set 
            {
                base["State"] = value; Save();
            }
        }
        public string ZipCode 
        {
            get 
            {
                return ((string)(base["ZipCode"]));
            }
            set 
            {
                base["ZipCode"] = value; Save();
            }
        }
        public string PhoneNumber 
        {
            get 
            {
                return ((string)(base["PhoneNumber"]));
            }
            set 
            {
                base["PhoneNumber"] = value; Save();
            }
        }
        public string SemesterID 
        {
            get 
            {
                return ((string)(base["SemesterID"]));
            }
            set 
            {
                base["SemesterID"] = value; Save();
            }
        }

        static public  ProfileInformation GetProfile(string username)
        {
            return Create(username) as ProfileInformation;
        }

        internal static ProfileInformation NewUser
        {
            get { return System.Web.HttpContext.Current.Profile as ProfileInformation; }
        }

    }
}

Here is the UserProfile Get Method:

[Authorize]
        public ActionResult UserProfile()
        {
            string id = User.Identity.Name.ToString();
            MembershipUser user = Membership.GetUser(id);

           var model = new UserViewModel();

            UserInformation userInf = new UserInformation();
            userInf.Username = user.UserName;
            userInf.Email = user.Email;

            ProfileInformation currentProfile = ProfileInformation.GetProfile(user.UserName);

            model.Profile = currentProfile;
            model.UserInf = userInf;    

            return View(model);

        }

As you can see I'm using a View Model to display the view. The View Model is this:

 public class UserViewModel
    {

        public UserInformation UserInf{ get; set; }
        public ProfileInformation Profile { get; set; }


    }

And finally the HttpPost Method for the Update UserProfile is:

[Authorize]
        [HttpPost]
        public ActionResult UserProfile(UserViewModel model)
        {
            ProfileInformation currentProfile = ProfileInformation.GetProfile(User.Identity.Name.ToString());

            ProfileInformation.CurrentUser.FirstName = model.Profile.FirstName;
            currentProfile.LastName = model.Profile.LastName;
            currentProfile.Street = model.Profile.Street;
            currentProfile.Street2 = model.Profile.Street2;
            currentProfile.City = model.Profile.City;
            currentProfile.State= model.Profile.State;
            currentProfile.ZipCode = model.Profile.ZipCode;
            currentProfile.PhoneNumber = model.Profile.PhoneNumber;
            currentProfile.Save();

            MembershipUser user = Membership.GetUser(User.Identity.Name.ToString());
            user.Email = model.UserInf.Email;
            Membership.UpdateUser(user);


            TempData["SuccessMessage"] = "Your Profile information has been saved";
            ViewBag.Title = "My Profile";
            return RedirectToAction("ProfileUpdated");
        }

The problem is I can't figure out where the error is coming from. The form renders I input valid information and then I'm told "The settings property 'FirstName' was not found." It's like the UserViewModel is attempting to update the ProfileInformation.cs (custom profile object) but it's saying it can't find the object... I'm thinking it's because it doesn't have a profile to update... but I can't get to the error before it posts... so frustrating! Any help would be greatly appreciated!!! Thanks for taking the time to check this out.

I'll also leave you with my Register method which uses the custom Profile object successfully and even updates the name property to "ScoobyDoo" for testing purposes :)

[HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretAnswer);

                if (createStatus == MembershipCreateStatus.Success)
                {

                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                    Roles.AddUserToRole(model.UserName, "Student");
                    ProfileInformation.NewUser.Initialize(model.UserName, true);
                    ProfileInformation.NewUser.Save();

                    string currentSemesterID = CurrentSemester;
                    ProfileInformation profile = ProfileInformation.GetProfile(model.UserName);
                    profile.SemesterID = currentSemesterID;
                    profile.FirstName = "ScoobyDoo";

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2024-10-26 04:12:27

您可能需要提供您自己的配置文件实现...如果您已经这样做了,您是否已将自定义提供程序类型添加到“web.config”中?

示例:

<profile defaultProvider="TableProfileProvider" enabled="true" inherits="YourNamespace.YourClass, YourNamespace">

很棒的 MSDN 文章此处此处,以及此处

我希望这些能让你上路!

You may need to provide the your own profile implementation... If you have done that, have you added your custom provider type to your "web.config"?

Example:

<profile defaultProvider="TableProfileProvider" enabled="true" inherits="YourNamespace.YourClass, YourNamespace">

Great MSDN article here, here, and here.

I hope these get you on your way!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文