2016-06-10 58 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的新手,我正在尝试了解这个在线社区。你的意见真的有帮助。我编辑了这篇文章。 –