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

asp.net core-将主页上的电子邮件表格发送到另一个注册表格

(asp.net core - Send email form at homepage to another register form)

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

我希望主页上的电子邮件表单能够在单击按钮时转到注册页面,然后在注册页面的电子邮件字段中已经填写了输入的电子邮件地址。有谁知道如何最好地做到这一点?我使用.net核心MVC制作网站,并使用身份框架。

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

我希望主页上的电子邮件表单能够在单击按钮时转到注册页面,然后在注册页面的电子邮件字段中已经填写了输入的电子邮件地址。

根据你的描述,我假设单击主页上的“注册”按钮后,它将向控制器提交表单(包含电子邮件字段),然后重定向到“注册”页面。如果是这样,你可以参考以下示例代码:

主页中的代码:

    <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>

家庭控制器中的代码:

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

“注册”页面中的代码:从TempData或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>

屏幕截图如下:

在此处输入图片说明

以下是一些有关传递数据的相关文章[视图到控制器,控制器到视图和控制器到控制器],你可以参考它们:

将数据传递到视图

传递数据[在ASP.NET MVC中从视图到控制器,从控制器到视图和从控制器到控制器](类似于在Asp.net核心中使用它)

ASP.NET Core中的会话和状态管理