ASP.Net MVC 3 如何更新用户帐户?

发布于 2024-12-05 08:13:50 字数 3620 浏览 0 评论 0原文

我想更新我的项目中的用户帐户。我有一个这样的视图:

@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>
}

控制器是这样的:

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
        ...
    }

我可以在 Sytem_User 表中更新 Office,但如何更新 aspnet_membership (用于更新评论、电子邮件、isLocked、isApproved)?

我认为我必须使用

membership.UpdateUser(MembershipUser User)

,但有人能给我一个如何使用membership.UpdateUser 的例子吗?

现在我可以更新用户评论和用户电子邮件,我使用这个:

 var usermembership = Membership.GetUser(UserId);

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

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

但是,我无法更新用户锁定和用户批准。如何更新那些?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆流年 2024-12-12 08:13:50

Membership.UpdateUser 方法

使用指定用户的信息更新数据库。

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

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);
鸢与 2024-12-12 08:13:50

查看 http://msdn。 microsoft.com/en-us/library/system.web.security.membership.updateuser.aspx

可能是 ASP.Net MVC 3 Membership.UpdateUser(MembershipUser 用户)

嗯 - 我认为你走在正确的轨道上。也许可以看看使用会员资格的应用程序的示例。

这里我使用 WebForms 来更改密码:

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

祝你好运。

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.

你如我软肋 2024-12-12 08:13:50

我认为一种方法是编写您的 onwmembershipuser 实现。

请查看这篇文章“如何:实现自定义成员资格用户”

如果您不喜欢实现每个方法,您可以创建一个后代类并仅编写行为不同所需的方法,然后在 web.config 中声明您的新提供程序

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文