将查询字符串传递到注册表单

时间:2018-06-18 17:19:41

标签: asp.net-mvc

我将按照以下方式订阅订阅流程:

  • 用户进入定价页面,选择计划,重定向到../注册?planId = 1
  • 用户注册用户名和密码,转发到结算页面,需要是../订阅/结算?planId = 1
  • 我在RegisterViewModel中添加了一个整数planId

在定价页面上,我的链接正常工作。

对于寄存器控制器,我有:

[AllowAnonymous]
public ActionResult Register(RegisterViewModel model, int planId)
{
    if (Request.IsAuthenticated) {
        return RedirectToAction("Pricing", "Home");
    }
    RegisterViewModel model1 = new RegisterViewModel();

    model1.planId = Convert.ToInt32(Request.QueryString["planId"]);
    return View(model1);

}

在Register视图中我有:

@Html.HiddenFor(m => m.planId)

但是,每次运行应用程序时,此值都是空白的。如果我可以将planId作为注册表单提交的一部分包含在内,那么我认为我可以将控制器重定向到" ../ Subscription / Billing?planId = 1"注册后。

这是当前的寄存器发布控制器,我认为我只需要将planid添加到redirectToAction:

 [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Role = "Admin", ReportsTo = "", ActiveUntil = DateTime.Now.AddDays(-1) };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://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>");

                    return RedirectToAction("Pricing", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

我的问题是,如何让planid从查询字符串传递到登录表单中的隐藏字段?

1 个答案:

答案 0 :(得分:1)

您可以尝试model1.planId = planId