asp.net mvc 没有向之前那样自动为我们生成可以直接操作profile的ProfileCommon类,所以我们要自己写一个。
当然这还是比较容易的。
ProfileController.cs
[AcceptVerbs("POST")] public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip) { MembershipUser user = Membership.GetUser(); ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
profile.Company = company; profile.Phone = phone; profile.Fax = fax; profile.City = city; profile.State = state; profile.Zip = zip; profile.Save();
return RedirectToAction("Index", "Account"); } |
web.config:
这里需要注意的是,需要去除掉自定义在<properties>中的所有属性,因为我们在之后的ProfileCommon里自定义了相应的属性。
同时在<profile>中添加
<profile
inherits="ProfileExample.Models.ProfileCommon"
defaultProvider="AspNetSqlProfileProvider"> | |
实现的ProfileCommon类
public class ProfileCommon : ProfileBase { public virtual string Company { get { return ((string)(this.GetPropertyValue("Company"))); } set { this.SetPropertyValue("Company", value); } }
public virtual string Phone { get { return ((string)(this.GetPropertyValue("Phone"))); } set { this.SetPropertyValue("Phone", value); } }
public virtual string Fax { get { return ((string)(this.GetPropertyValue("Fax"))); } set { this.SetPropertyValue("Fax", value); } }
public virtual string City { get { return ((string)(this.GetPropertyValue("City"))); } set { this.SetPropertyValue("City", value); } }
public virtual string State { get { return ((string)(this.GetPropertyValue("State"))); } set { this.SetPropertyValue("State", value); } }
public virtual string Zip { get { return ((string)(this.GetPropertyValue("Zip"))); } set { this.SetPropertyValue("Zip", value); } }
public virtual ProfileCommon GetProfile(string username) { return Create(username) as ProfileCommon; } }
| |
转自http://kogeiman.ixiezi.com/2011/01/13/%E5%AE%9E%E7%8E%B0-profile-provider-in-asp-net-mvc/