c# - 在表单中使用 AspNetUsers.Id
问题描述
我想要的是一个人注册(使用现有的注册页面)然后被定向到他们的 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")
}
解决方案
你有两个主要问题:
在注册操作中,您使用 ViewBag 传递 userId 值,然后使用 RedirectToAction。您不能在这种情况下使用 ViewBag,因为价值会丢失。在这里检查
您没有在输入框 [CreateProfile 视图] 上分配 UserId 值。
要解决它:
在 CreateProfile Get Action 中捕获 UserId 值,然后在 ViewBag 中分配以将值传递给 View。两种形式:
一个。使用MVC绑定,设置一个变量:
// Get: Admin/CreateProfile
public ActionResult CreateProfile(string UserId) // <== here
{
ViewBag.UserId = UserId;
return View();
}
湾。使用请求对象获取价值。
// Get: Admin/CreateProfile
public ActionResult CreateProfile()
{
ViewBag.UserId = Request.Params["UserId"]; // <== here
return View();
}
可选:您可以使用注册操作中的会话对象保存 UserId 值。也许我会喜欢这个,因为避免在代码之前。在这里检查。
最后使用@Value 在 CreateProfile 视图中的输入框上分配 UserId 值:
新的 { htmlAttributes = new { @class = "form-control", @Value= ViewBag.UserId }
IE:
<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", @Value= ViewBag.UserId } })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
推荐阅读
- java - 需要一些帮助将 Java 代码 Android Studio 移植到 C# Visual Studio
- kubernetes - 如何定期将 shell 脚本的结果设置为 Kubernetes Cronjob 的参数
- r - 根据来自不同数据框的标签向数据框添加列
- rest - 断言时出现错误预期:可迭代包含 [“IT”] 实际:IT
- node.js - 接收 TS2304:找不到名称“文件”| TS2304:运行“npm run tsc”命令时找不到名称“ReadableStream”
- excel - 如何在 VBA 中锁定 msgbox 弹出窗口
- r - How to do use `replace` properly in complex cases?
- arm - ARMv7-M 手册中没有“beq”或“bne”指令?
- json - 使用 mapbox datasets api 时总是收到“未找到”消息
- arrays - 添加维度numpy数组