我是Asp.Net MVC的新手,正在处理注册和登录表单。我创建了MVC默认应用程序,如您所知,它带有Users, User Roles, User Claim, User Login and Roles
的内置成员发送表。
我在Roles
表普通用户&中创建了2个角色 VIP用户
现在我已经在Resgiter
中复制并粘贴了相同的AccountController
ActionMethod,并在重新启动时分配了角色。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> NormalUser(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddToRole(user.Id, "Normal");
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
[AllowAnonymous]
public ActionResult VIPUser()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> VIPUser(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddToRole(user.Id, "VIP");
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
查看此视图代码适用于NormalUser,我也是VIPUser的相同视图代码。和模型类是默认的。
@model FypStore.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Template.cshtml";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("NormalUser", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, 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">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
对于上面的代码,我必须创建2个视图,这意味着2个表单。 1表示普通用户,1表示VIP用户。
我如何以1种形式执行此操作?我的意思是只有1个用户名,密码,确认密码字段,附加字段为2个单选按钮o VIP o Normal
。无论用户选择哪个角色,都将其分配给他并注册。