我想更新项目中的用户帐户。我有这样的观点:
@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);
但是,我无法更新用户锁定和用户批准。如何更新thoose?
谢谢
答案 0 :(得分:2)
使用指定用户的信息更新数据库。
MembershipUser u = Membership.GetUser(User.Identity.Name);
u.Email = someValue;
Membership.UpdateUser(u);
答案 1 :(得分:0)
查看http://msdn.microsoft.com/en-us/library/system.web.security.membership.updateuser.aspx
ASP.Net MVC 3 Membership.UpdateUser(MembershipUser user)
的可能欺骗 嗯 - 我认为你走在正确的轨道上。也许可以浏览一下使用会员资格的应用程序示例。这里我使用WebForms更改密码:
user = Membership.GetUser(txtUsername.Text);
string generatedPassword = user.ResetPassword();
user.ChangePassword(generatedPassword, txtPassword.Text);
祝你好运。
答案 2 :(得分:0)
我认为有一种方法可以编写你的onw membershipuser实现。
请看这篇文章"How to: Implement a Custom Membership User"。
如果你不想实现每个方法,你可以创建一个后代类,只写出你需要表现不同的方法,然后在web.config中声明你的新提供者