【发布时间】:2019-02-28 13:48:27
【问题描述】:
我想要的是一个人注册(使用现有的注册页面)然后被定向到他们的 AspNetUsers.Id = UserId 的表单(在我的 CreateProfile 页面上)。 CreateProfile 是您成功注册后被定向到的页面。正如您在地址栏中的图像中看到的那样,您可以看到用户 ID,但它不会出现在输入框中。
帐户控制器
public async Task<ActionResult> Register(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)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// Registered user is given the applicant role by default
UserManager.AddToRole(user.Id, "Applicant");
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// 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>");
ViewBag.UserId = user.Id;
//return RedirectToAction("Index", "Home");
return RedirectToAction("CreateProfile", new { controller = "Admin", UserId = user.Id });
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
创建配置文件视图
@model NAA.Data.Profile
@{
ViewBag.Title = "CreateProfile";
}
<h2>Create Profile</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Profile</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ApplicantName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ApplicantName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ApplicantName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ApplicantAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ApplicantAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ApplicantAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserId, new { htmlAttributes = new { @class = "form-control", } })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "GetProfile", new { controller = "Profile", action = "GetProfile" })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
-
您可以使用 CreateProfile 操作及其查看代码更新您的问题吗?
-
我已经添加了你推荐 Luis 的代码。
-
为了以后的场合贴代码,避免使用图片。
标签: c# asp.net-mvc