答案 0 :(得分:2)
我创建了一个引用MembershipUser的模型,但也允许创建和编辑用户。
namespace MyProject.Models
{
public class AccountUser
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual Guid UserId { get; set; }
[Display(Name="User Name")]
public virtual string UserName { get; set; }
[Display(Name = "E-mail")]
public virtual string Email { get; set; }
public virtual string Password { get; set; }
[Display(Name = "Approved")]
public virtual bool IsApproved { get; set; }
/* contructors based on string GUID or actual */
public AccountUser() { }
public AccountUser(string UID)
{
UserId = new Guid(UID);
Initialize();
}
public AccountUser(Guid UID)
{
UserId = UID;
Initialize();
}
/* loads Membership User into model to access other properties */
public virtual MembershipUser User
{
get
{
// note that I don't have a test for null in here,
// but should in a real case.
return Membership.GetUser(UserId);
}
}
/* do this once when opening a user instead of every time you access one of these three *
* as well as allow override when editing / creating */
private void Initialize()
{
UserName = User.UserName;
Email = User.Email;
IsApproved = User.IsApproved;
}
}
}
通过这个构建,我用我的默认数据Context创建了一个Controller,允许它为我创建脚手架。然后我从Controller中删除了Context。
namespace MyProject.Controllers
{
[Authorize]
public class AccountUserController : Controller
{
public ViewResult Index()
{
var memberList = Membership.GetAllUsers();
var model = new List<AccountUser>();
foreach (MembershipUser user in memberList)
{
model.Add(new AccountUser(user.ProviderUserKey.ToString()));
}
return View(model);
}
public ViewResult Details(Guid id)
{
AccountUser accountuser = new AccountUser(id);
return View(accountuser);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(AccountUser myUser)
{
if (ModelState.IsValid)
{
Membership.CreateUser(myUser.UserName, myUser.Password, myUser.Email);
return RedirectToAction("Index");
}
return View(myUser);
}
public ActionResult Edit(Guid id)
{
AccountUser accountuser = new AccountUser(id);
return View(accountuser);
}
[HttpPost]
public ActionResult Edit(AccountUser accountuser)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
return View(accountuser);
}
public ActionResult Delete(Guid id)
{
AccountUser accountuser = new AccountUser(id);
return View(accountuser);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(Guid id)
{
AccountUser accountuser = new AccountUser(id);
Membership.DeleteUser(accountuser.User.UserName);
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
//db.Dispose();
base.Dispose(disposing);
}
}
}
视图应该都非常简单,但这里是一致的
@model MyProject.Models.AccountUser
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AccountUser</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这总体上有点棘手,主要是让模型正确,允许您从成员资格中读取以及创建一组合理的视图。我实际上是手工完成了大部分工作,但这样可以节省一些时间。请注意,我省略了编辑密码或角色,但如果你做到这一点,你就不应该太离谱。
以下链接很有帮助:
答案 1 :(得分:1)
创建一个新项目mvc3并从软件包管理器下载这个很棒的项目:
PM> Install-Package MvcProjectAwesome