0

What I'm wanting is for a person to register (using existing register page) then get directed to a form where their AspNetUsers.Id = UserId (on my CreateProfile page). CreateProfile is the page you are directed to when you have successfully registered. As you can see in the image in the address bar you can see the user id but it won't appear in the input box.

AccountController

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);
        }

CreateProfile View

@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

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Raj R96
  • 50
  • 1
  • 10

1 Answers1

0

you have two main problems:

  1. In Register action you pass userId value using ViewBag and later use RedirectToAction. You cannot use ViewBag for this scenario, because value will loss. Check here

  2. You don't assign UserId value on input box [CreateProfile view].

To solve it:

Capture UserId value in CreateProfile Get Action and later assign in ViewBag to pass value to View. Two forms:

a. Use MVC bind, set a variable:

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

b. Use request object to get value.

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

Optional: you could save UserId value using Session Object in Register Action. Maybe I would like this because avoid before code. Check here.

Finally assign UserId value on input box in CreateProfile view using @Value:

new { htmlAttributes = new { @class = "form-control", @Value= ViewBag.UserId }

I.E:

<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>
LuisEduardox
  • 364
  • 4
  • 9