2016-06-10 61 views
0

我正在處理我的項目的註冊部分。一旦用戶想要註冊一個帳戶,我希望用戶也填寫他們的地址,城市,郵政編碼,名字,姓氏和一些其他的東西(在下面的模型中描述)。如何在ASP.NET MVC中編輯註冊部分?

我該如何解決這個問題?

我試圖改變如下代碼:

~/Controllers/AccountController.cs

// 
// GET: /Account/Register 

[AllowAnonymous] 
public ActionResult Register() 
{ 
    return View(); 
} 

// 
// POST: /Account/Register 

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public ActionResult Register(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // Attempt to register the user 
     try 
     { 
      WebSecurity.CreateUserAndAccount(model.UserName, model.Password); 
      WebSecurity.Login(model.UserName, model.Password); 
      return RedirectToAction("Index", "Home"); 
     } 
     catch (MembershipCreateUserException e) 
     { 
      ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

我注意到有POST方法public ActionResult Register(RegisterModel model) {}其中僅保存表稱爲UserProfilewebpages_Membership的用戶名和密碼中的代碼的一部分,看起來像這樣:

try 
{ 
    WebSecurity.CreateUserAndAccount(model.UserName, model.Password); 
    WebSecurity.Login(model.UserName, model.Password); 
    return RedirectToAction("Index", "Home"); 
} 

我不應該改變WebSecurity.CreateUserAndAccount()函數,因爲這隻需要幾個參數,一個用戶名和一個密碼以及其他的東西。

,但我卻改變我的模型和視圖以下幾點:

~/Views/Account/Register.cshtml

@model Rent_a_Car_MVC.Models.RegisterModel 
@{ 
    ViewBag.Title = "Registreren"; 
} 

<h2>@ViewBag.Title.</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    <h4>Maak een account aan.</h4> 
    <hr /> 
    @Html.ValidationSummary() 

    <div class="form-group"> 
     @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.FirstName, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.LastName, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Address, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.PostalCode, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.PostalCode, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.City, new { @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Register" /> 
     </div> 
    </div> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

~/Models/AccountModel.cs

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "Gebruikersnaam")] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "{0} moet minstens {2} letters bevatten.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Wachtwoord")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Bevestig wactwoord")] 
    [Compare("Password", ErrorMessage = "Wachtwoord en bevestigings wachtwoord komen niet overeen.")] 
    public string ConfirmPassword { get; set; } 
    [Required] 
    [Display(Name = "Voornaam")] 
    public string FirstName { get; set; } 
    [Required] 
    [Display(Name = "Achternaam")] 
    public string LastName { get; set; } 
    [Required] 
    [Display(Name = "Adres")] 
    public string Address { get; set; } 
    [Required] 
    [Display(Name = "Postcode")] 
    public string PostalCode { get; set; } 
    [Required] 
    [Display(Name = "Woonplaats")] 
    public string City { get; set; } 
} 

我是不是做正確嗎?我應該只需要更換控制器,如果是的話,我將如何處理這個問題?

+1

[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) - 關鍵字:**最小** –

+0

您是否更新了數據庫模型? –

回答

3

This link讓一步一步的指示:

下面,我已經包括如下步驟的簡要,你將需要完成的文章中概述:

  1. 運行應用程序並註冊用戶
  2. 啓用實體框架遷移
  3. 爲要存儲的配置文件信息(地址,城市,郵政編碼等)添加屬性。
  4. 添加的遷移修改數據庫
  5. 修改應用程序添加字段爲新的屬性
  6. 更新的註冊行動中的AccountController存儲新特性,以及對用戶
  7. 運行應用程序並註冊一個用戶再次。您將看到測試框爲您新添加的屬性輸入值。 8.顯示頁面上的配置文件信息(可選步驟)

我相信這是你正在嘗試做的。如果我誤解了,請告訴我。

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/12644289) – Chris

+1

感謝Chris的幫助!我是StackOverflow的新手,我正在嘗試瞭解這個在線社區。你的意見真的有幫助。我編輯了這篇文章。 –