ntep vodka
ntep vodka

Reputation: 735

ASP.Net MVC 3 how to UpdateUser account?

i want to update user account in my project. i have a view like this :

@using (Html.BeginForm("edit/" + @Model.SysUsers[0].UserID, "cpanel/sysuser",  
FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>@Model.SysUsers[0].UserID</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SysUsers[0].UserName)
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserEmail)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SysUsers[0].UserEmail)
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserEmail)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserComment)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SysUsers[0].UserComment)
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserComment)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserLocked)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SysUsers[0].UserLocked)
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserLocked)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserApproved)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SysUsers[0].UserApproved)
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserApproved)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SysUsers[0].UserOffice)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.SysUsers[0].UserOffice, new 
        SelectList(Model.GetawayOffice, "OfficeCode", "OfficeDesc", 
        Model.SysUsers[0].UserOffice))
        @Html.ValidationMessageFor(model => model.SysUsers[0].UserOffice)
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

and the controller is like this :

public ActionResult Edit(string id) 
    {
        model.SysUsers = repo.SelectSystemUser(id).ToList();
        model.Office = reps.Office().ToList();

        return View(model);
    }
[HttpPost]
    public ActionResult Edit(string id, FormCollection collection)
    {
        //update in System_User Table
        Guid UserId = new Guid(id.ToString());
        var SysUser = db.System_User.Single(s => s.User_UserId == UserId);
        SysUser.User_Office = collection["SysUsers[0].UserOffice"];

        //update aspnet_membership, like comment, email, isLocked, isApproved
        ...
    }

i can update Office in Sytem_User table, but how can I update aspnet_membership (its for updting comment, email, isLocked, isApproved) ?

i think that i must use

membership.UpdateUser(MembershipUser User)

but can someone give me an example how to use membership.UpdateUser ?

now i can update user comment and user email, i use this :

 var usermembership = Membership.GetUser(UserId);

 usermembership.Comment = collection["SysUsers[0].UserComment"];
 Membership.UpdateUser(usermembership);

 usermembership.Email = collection["SysUsers[0].UserEmail"]; 
 Membership.UpdateUser(usermembership);

but, i cant update user locked and user approve. how to update thoose ?

thank you

Upvotes: 0

Views: 2095

Answers (3)

Iridio
Iridio

Reputation: 9271

I think a way would be the one to write your onw membershipuser implementation.

Look at this post "How to: Implement a Custom Membership User".

If you don't like to implement every method you can create a descendant class and write only the methods you need to behave differently, then declare your new provider in the web.config

Upvotes: 0

Dave Mateer
Dave Mateer

Reputation: 6626

Have a look at http://msdn.microsoft.com/en-us/library/system.web.security.membership.updateuser.aspx

Possible dupe of ASP.Net MVC 3 Membership.UpdateUser(MembershipUser user)

Hmm - I think you are on the right track. Perhaps have a look around for examples of apps that use membership.

Here I am using WebForms to change a password:

        user = Membership.GetUser(txtUsername.Text);
        string generatedPassword = user.ResetPassword();
        user.ChangePassword(generatedPassword, txtPassword.Text);

Good luck.

Upvotes: 0

CD..
CD..

Reputation: 74096

Membership.UpdateUser Method

Updates the database with the information for the specified user.

MembershipUser u = Membership.GetUser(User.Identity.Name);
u.Email = someValue;
Membership.UpdateUser(u);

Upvotes: 2

Related Questions