1

我有一个使用 ASP.Net MVC 3 的项目,现在我想做用户管理。我想让它像这样:http ://mrgsp.md:8080/awesome/user

怎么做那个用户管理?

多谢

4

2 回答 2

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

    }
}

构建完成后,我使用默认数据上下文创建了一个控制器,以允许它为我创建脚手架。然后我从控制器中删除了上下文。

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>

总体而言,这肯定有点棘手,主要是让模型正确,以允许您从 Membership 中读取数据并创建一组合理的视图。实际上我大部分都是手工完成的,但这会节省你一些时间。请注意,我省略了编辑密码或角色,但如果你做到了这一点,你应该不会太远。

以下链接很有帮助:

于 2012-05-09T19:02:45.597 回答
1

创建一个新项目 mvc3 并从包管理器下载很棒的项目:

PM> Install-Package MvcProjectAwesome 
于 2011-09-27T07:38:16.173 回答