Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a Model which references the MembershipUser but also allows for creating and editing users.</p> <pre><code>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; } } } </code></pre> <p>With this built, I created a Controller with my default data Context to allow it to create scaffolding for me. Then I removed the Context from the Controller.</p> <pre><code>namespace MyProject.Controllers { [Authorize] public class AccountUserController : Controller { public ViewResult Index() { var memberList = Membership.GetAllUsers(); var model = new List&lt;AccountUser&gt;(); 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); } } } </code></pre> <p>The Views should all be pretty straight forward, but here's one for consistency</p> <pre><code>@model MyProject.Models.AccountUser @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;AccountUser&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.UserName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.UserName) @Html.ValidationMessageFor(model =&gt; model.UserName) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Password) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.PasswordFor(model =&gt; model.Password) @Html.ValidationMessageFor(model =&gt; model.Password) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Email) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Email) @Html.ValidationMessageFor(model =&gt; model.Email) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>It was definitely a little tricky overall, mostly with getting the Model correct to allow you to read in from Membership as well as get a reasonable set of Views created. I actually did most of them by hand, but this will save you some time. Note that I omitted editing Password or Roles, but if you get this far, you shouldn't be too far off.</p> <p>The following links were helpful:</p> <ul> <li><a href="https://stackoverflow.com/questions/901620/alternative-user-management-in-asp-net-mvc">Alternative User management in ASP.NET MVC</a></li> <li><a href="http://joymonscode.blogspot.com/2012/01/creating-simple-aspnet-mvc-3-razor.html" rel="nofollow noreferrer">http://joymonscode.blogspot.com/2012/01/creating-simple-aspnet-mvc-3-razor.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload