ASP.Net MVC 3 Membership.UpdateUser(MembershipUser user)

£可爱£侵袭症+ 提交于 2019-12-13 02:48:01

问题


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 = collection["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 :

  1. user.UserName is read only, i cant update this one.
  2. i get user.Comment value, but its not update.
  3. 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,


回答1:


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.




回答2:


I would suggest using the Membership Provider primarily for authentication and build all the other maintenance etc. methods into your User's business objects.



来源:https://stackoverflow.com/questions/7468179/asp-net-mvc-3-membership-updateusermembershipuser-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!