开发者

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

开发者 https://www.devze.com 2023-04-06 00:11 出处:网络
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 trou

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 :

  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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号