问题: 我已经创建了一个自定义配置文件对象,如Joel here所述。然后我使用Jeremy的方法(here)来扩展自定义配置文件,以允许我使用生成用户并设置这些值。然后,我创建了一个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(自定义配置文件对象),但它说它无法找到对象......我在想它是因为它没有要更新的配置文件......但我在发布之前无法得到错误......太令人沮丧了!任何帮助将不胜感激!!!感谢您抽出宝贵时间来检查这一点。
我还会给你注册成功使用自定义Profile对象的Register方法,甚至将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);
}