Warm tip: This article is reproduced from serverfault.com, please click

Send email form at homepage to another register form

发布于 2020-12-01 12:09:59

I want an email form on the homepage that goes to the register page when the button is clicked and then on the register page the email field is already filled in with the email address entered. Does anyone know how best to do this? I make the website in .net core MVC and use the identity framework.

Questioner
Robigo
Viewed
0
Zhi Lv 2020-12-02 14:27:51

I want an email form on the homepage that goes to the register page when the button is clicked and then on the register page the email field is already filled in with the email address entered.

Based on your description, I assume after click the Register button in the Home page, it will submit the form (which contains the email field) to the controller, then, redirect to the Register page. If that is the case, you could refer the following sample code:

Code in the Home page:

    <form asp-action="HomePage" asp-controller="Home"> 
        <div class="form-group">
            <label for="email" class="control-label">Email</label>
            <input name="email" class="form-control" /> 
        </div> 
        <div class="form-group">
            <input type="submit" value="Register" class="btn btn-primary" />
        </div>
    </form>

Code in the Home Controller:

    public IActionResult HomePage()
    {
        return View();
    }

    [HttpPost]
    public IActionResult HomePage(string email)
    {
        //if email not null, transfer the email to next action, using TempData, Session and so on.
        if (email != null)
        {
            TempData["email"] = email;
        }
        return RedirectToAction(nameof(Register),"Home");
    }

    public IActionResult Register()
    {
        //you could use the following code to check whether the tempate data is exist. then do something.
        //if (TempData["email"] != null)
        //{
        //    var value = TempData["email"];
        //}
        return View();
    }

Code in the Register page: Get the email from the TempData or Session.

    <form asp-action="Register"> 
        <div class="form-group">
            <div class="form-group">
                <label for="email" class="control-label">Email</label>
                <input name="email" class="form-control" value='@TempData["email"]' />
            </div>

            @{ 
                var value = TempData["email"]; //get the data from TempData or session.
            }
        </div> 
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>

The screenshot as below:

enter image description here

Here are some related articles about Passing Data [View-to-Controller, Controller-to-View & Controller-to-Controller], you could refer to them:

Passing data to views

Passing Data [View-to-Controller, Controller-to-View & Controller-to-Controller in ASP.NET MVC](It is similar to use it in Asp.net core)

Session and state management in ASP.NET Core