2011-02-26 34 views
6

問題: 我已經創建如由Joel here描述的定製配置文件對象。然後,我使用Jeremy的方法(here)擴展自定義配置文件,以允許我使用生成用戶並設置這些值。然後,我創建了一個ViewModel來顯示Memeberhip信息和配置文件信息,以便用戶可以更新他們的會員信息(電子郵件)和配置文件信息。 **視圖顯示所有我在查看更新後的信息輸入,並點擊保存等領域,在那裏我碰到下面的錯誤資料對象+視圖模型+更新用戶配置文件MVC C#

System.Configuration.SettingsPropertyNotFoundException的:設置屬性「姓」沒有被發現。 **

這裏是我的自定義配置文件對象(模型):

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; } 
     } 

    } 
} 

下面是用戶配置獲取方法:

[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); 

     } 

正如你可以看到我使用一個View Model來顯示視圖。 視圖模型是這樣的:

public class UserViewModel 
    { 

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


    } 

最後的更新用戶配置的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"); 
     } 

問題是我無法揣摩出錯誤是來自。表單呈現我輸入有效的信息,然後我被告知「設置屬性'名字'找不到。」這就像UserViewModel試圖更新ProfileInformation.cs(自定義配置文件對象),但它說它找不到對象...我想這是因爲它沒有配置文件來更新...但我在發佈之前無法獲得錯誤...非常令人沮喪!任何幫助將不勝感激!!!感謝您花時間檢查了這一點。

我也會離開你成功地使用自定義配置文件對象,甚至我的註冊方法更新名稱屬性爲「史酷比」用於測試目的:)

[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); 
     } 

回答

1

您可能需要提供您的自己的配置文件實施...如果你已經這樣做了,你有沒有添加自定義提供程序類型到你的「web.config」?

例子:

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

大MSDN文章hereherehere

我希望這些能讓你順其自然!