【问题标题】:Using AspNetUsers.Id in a form在表单中使用 AspNetUsers.Id
【发布时间】: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")
}

Image of CreateProfile
Controller Action

【问题讨论】:

  • 您可以使用 CreateProfile 操作及其查看代码更新您的问题吗?
  • 我已经添加了你推荐 Luis 的代码。
  • 为了以后的场合贴代码,避免使用图片。

标签: c# asp.net-mvc


【解决方案1】:

你有两个主要问题:

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

  2. 您没有在输入框 [CreateProfile 视图] 上分配 UserId 值。

解决它:

在 CreateProfile Get Action 中捕获 UserId 值,然后在 ViewBag 中分配以将值传递给 View。两种形式:

一个。使用MVC绑定,设置一个变量:

// Get: Admin/CreateProfile
public ActionResult CreateProfile(string UserId) // <== here
{
    ViewBag.UserId = UserId;
    return View();
}

b.使用请求对象获取值。

// Get: Admin/CreateProfile
public ActionResult CreateProfile()
{
    ViewBag.UserId = Request.Params["UserId"]; // <== here
    return View();
}

可选:您可以在注册操作中使用会话对象保存 UserId 值。也许我会喜欢这个,因为避免在代码之前。 Check here

最后在 CreateProfile 视图中使用 @Value 为输入框分配 UserId 值:

new { htmlAttributes = new { @class= "表单控制", @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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2011-09-25
    • 2018-10-25
    • 2015-01-24
    • 1970-01-01
    • 2014-12-25
    • 2021-05-31
    相关资源
    最近更新 更多