首页 > 解决方案 > 在表单中使用 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")
}

CreateProfile
控制器操作的图像

标签: c#asp.net-mvc

解决方案


你有两个主要问题:

  1. 在注册操作中,您使用 ViewBag 传递 userId 值,然后使用 RedirectToAction。您不能在这种情况下使用 ViewBag,因为价值会丢失。在这里检查

  2. 您没有在输入框 [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>

推荐阅读