2011-09-27 108 views

回答

1

創建一個新項目MVC3,並從包管理器下載真棒項目:

PM> Install-Package MvcProjectAwesome 
+0

感謝答覆,但我在哪裏可以找到PM>安裝,包裝MvcProjectAwesome? –

+0

如果您使用Visual Studio 2010打開查看 - >其他窗口 - >程序包管理器控制檯。 否則您可以從網站http://awesome.codeplex.com/releases/view/66067 –

+0

下載Awesome項目感謝您的回覆。我已經爲用戶角色製作了checkboxlist,但是當我提交表單時我沒有獲得價值。 –

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

    } 
} 

這個內置的,我創建了一個控制器與我的默認數據上下文,讓它爲我創建腳手架。然後我從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> 

這肯定是有點棘手,總體而言,大多是與得到的模型正確,讓您從成員閱讀以及獲得一組合理的視圖。我其實大部分都是通過手工完成的,但這會爲您節省一些時間。請注意,我省略了編輯密碼或角色,但如果你得到這麼遠的話,你不應該離得太遠。

下面的聯繫是有益的: