查看您的 AccountModels.cs 文件。它包含
public class RegisterModel
{
// User name, Email Adress, Password, Password confirmation already there
// you can add something like below
[Required]
[Display(Name = "Nickname")]
public string Nickname { get; set; }
}
一旦模型中有新属性,您就需要更新视图。在 Views > Account > Register.cshtml 中,您应该添加
<div class="editor-label">
@Html.LabelFor(m => m.Nickname )
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Nickname )
@Html.ValidationMessageFor(m => m.Nickname )
</div>
完成后,您需要更新注册逻辑以使用新属性。进入AccountController并找到
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
//
// this would be a good place for you to put your code to do something with model.Nickname
//
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
如果您想将该信息保留到用户 ASP.NET 配置文件中,则需要在 Web.config 中使用此信息
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="Nickname" defaultValue="False" type="System.String" />
</properties>
</profile>
然后在你的代码中 - 你可以这样做
var userProfile = ProfileBase.Create(model.UserName);
在个人资料中获取/设置您的属性