Return page but leave input.

huangapple go评论53阅读模式
英文:

Return page but leave input

问题

以下是翻译好的代码部分:

我正在创建一个简单的登录表单。在按钮点击时,将触发以下函数:

public async Task<IActionResult> OnPostButton_Login(IFormCollection data)
{
    var emailAddress = Request.Form["emailaddress"];     
    var pass = Request.Form["password"];

    if (String.IsNullOrEmpty(emailAddress)) return Page();
    if (String.IsNullOrEmpty(pass)) return Page();

    await GetUser(emailAddress, pass);

    return Page();
}
如果用户在电子邮件或密码字段中没有输入任何内容,页面将重新加载 - 但也会清除所有输入。

如何从这个函数返回,但保持一切不变(只返回一个警告)?

请注意,我已将 HTML 编码的字符还原为正常字符。如果需要更多帮助,请告诉我。

英文:

I am creating a simple login form. upon button click this function is fired:

public async Task&lt;IActionResult&gt; OnPostButton_Login(IFormCollection data)
{
    var emailAddress = Request.Form[&quot;emailaddress&quot;];     
    var pass = Request.Form[&quot;password&quot;];

    if (String.IsNullOrEmpty(emailAddress)) return Page();
    if (String.IsNullOrEmpty(pass)) return Page();

    await GetUser(emailAddress , pass);

    return Page();
}

If the user doesn't type anything under either email or password, the page reloads - but also clears all inputs.

How can I return from this function but leave everything untouched (and just return an alert?)

答案1

得分: 0

请注意,以下是您要翻译的代码部分:

[BindProperty]
public string emailaddress { get; set; }

[BindProperty]
public string password { get; set; }

ModelState.AddModelError(string.Empty, "My message: Please provide an email address.");

public async Task OnGetAsync(string returnUrl = null)
{
    await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
}

public async Task&lt;IActionResult&gt; OnPostButton_Login(IFormCollection data)
{
    string emailAddress = Request.Form["emailaddress"].ToString();
    string pass = Request.Form["password"].ToString();

    if (String.IsNullOrEmpty(emailAddress))
    {
        ModelState.AddModelError(string.Empty, "My message: Please provide an email address.");
        return Page();
    }
    if (String.IsNullOrEmpty(pass))
    {
        ModelState.AddModelError(string.Empty, "My message: Please provide a password.");
        return Page();
    }

    await GetUser(emailAddress, pass);

    return Page();
}

private Task GetUser(string emailAddress, string pass)
{
    throw new NotImplementedException();
}
@page
@model LoginModel
@{
    ViewData["Title"] = "Log in";
}
<h1>@ViewData["Title"]</h1>
<div class="col-md-4">
    <section>
        <form id="account" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-floating">
                <input asp-for="emailaddress" class="form-control" autocomplete="username" aria-required="true" />
                <label asp-for="emailaddress" class="form-label"></label>
                <span asp-validation-for="emailaddress" class="text-danger"></span>
            </div>
            <div class="form-floating">
                <input asp-for="password" class="form-control" autocomplete="current-password" aria-required="true" />
                <label asp-for="password" class="form-label"></label>
                <span asp-validation-for="password" class="text-danger"></span>
            </div>
            <div>
                <input type="submit" value="Submit" asp-page-handler="Button_Login" />
            </div>
        </form>
    </section>
</div>

如您所需,这是代码的翻译部分。

英文:

> How can I return from this function but leave everything untouched (and just return an alert?)

Add the [BindProperty] attribute to the properties you want to reload into the returned page.

[BindProperty]
public string emailaddress { get; set; }

[BindProperty]
public string password { get; set; }

Use the ModelState.AddModelError method to add a string alert/message to the returned page.

ModelState.AddModelError(string.Empty, &quot;My message: Please provide an email address.&quot;);

Here's a stripped down version of a login page using your code:

Login.cshtml.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Areas.Identity.Pages.Account
{
    public class LoginModel : PageModel
    {
    [BindProperty]
    public string emailaddress { get; set; }

    [BindProperty]
    public string password { get; set; }

        public async Task OnGetAsync(string returnUrl = null)
        {
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
        }

        public async Task&lt;IActionResult&gt; OnPostButton_Login(IFormCollection data)
        {
            string emailAddress = Request.Form[&quot;emailaddress&quot;].ToString();
            string pass = Request.Form[&quot;password&quot;].ToString();

            if (String.IsNullOrEmpty(emailAddress))
            {
                ModelState.AddModelError(string.Empty, &quot;My message: Please provide an email address.&quot;);
                return Page();
            }
            if (String.IsNullOrEmpty(pass))
            {
                ModelState.AddModelError(string.Empty, &quot;My message: Please provide a password.&quot;);
                return Page();
            }

            await GetUser(emailAddress, pass);

            return Page();
        }

        private Task GetUser(string emailAddress, string pass)
        {
            throw new NotImplementedException();
        }
    }
}

Login.cshtml

@page
@model LoginModel
@{
    ViewData[&quot;Title&quot;] = &quot;Log in&quot;;
}
&lt;h1&gt;@ViewData[&quot;Title&quot;]&lt;/h1&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
    &lt;section&gt;
        &lt;form id=&quot;account&quot; method=&quot;post&quot;&gt;
            &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
            &lt;div class=&quot;form-floating&quot;&gt;
                &lt;input asp-for=&quot;emailaddress&quot; class=&quot;form-control&quot; autocomplete=&quot;username&quot; aria-required=&quot;true&quot; /&gt;
                &lt;label asp-for=&quot;emailaddress&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
                &lt;span asp-validation-for=&quot;emailaddress&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-floating&quot;&gt;
                &lt;input asp-for=&quot;password&quot; class=&quot;form-control&quot; autocomplete=&quot;current-password&quot; aria-required=&quot;true&quot; /&gt;
                &lt;label asp-for=&quot;password&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
                &lt;span asp-validation-for=&quot;password&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; asp-page-handler=&quot;Button_Login&quot; /&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/section&gt;
&lt;/div&gt;

Return page but leave input.

Alternative, manual control of the submit process

Here's a full list of what can be returned by a post method in a Razor page. You can take control of the submit process through JavaScript on the client and using the fetch API.

You can post to the OnPostButton_Login and then return with a content result (e.g. return Content(&quot;This is a message.&quot;); to the Promise callback of the fetch method. In this process, your page does not reload but just the input values in the form are posted. Here's a SO post of controlling the submit process.

huangapple
  • 本文由 发表于 2023年3月31日 20:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定