i have a project that use MVC3. in my project, i have a page that user can edit their account (UserComment, UserEmail, IsLocked, IsApproved). i already make the View for Edit Account. i have some trouble to make the Edit Controller. here's my code:
[HttpPost]
public ActionResult Edit(string id, FormCollection collection)
{
id = id.Replace("+", " ");
var user = Membership.GetUser();
Guid UserGUID = new Guid(user.ProviderUserKey.ToString());
var SysUser = db.System_User.Single(u => u.User_UserId == UserGUID);
//this is for updating User Office in my System_user table
SysUser.User_Office = collection["SysUsers[0].UserOffice"];
//this is for updating User Account in aspnet_membership table
user.UserName = col开发者_JAVA技巧lection["SysUsers[0].UserName"];
Membership.UpdateUser(user);
user.Comment = collection["SysUsers[0].UserComment"];
Membership.UpdateUser(user);
user.Email = collection["SysUsers[0].UserEmail"];
Membership.UpdateUser(user);
return View();
}
when i run my controller, i get some error like :
- user.UserName is read only, i cant update this one.
- i get user.Comment value, but its not update.
- i get error in my Edit View, it says "Object reference not set to an instance of an object." @using (Html.BeginForm("edit/" + @Model.SysUsers[0].UserID, "cpanel/sysuser", FormMethod.Post))
can anyone help me ?
thanks,
You cannot update the UserName of a user using ASP.NET Membership provider. You need to write some custom data access logic if you wish to update a username.
I would suggest using the Membership Provider primarily for authentication and build all the other maintenance etc. methods into your User's business objects.
精彩评论