英文:
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<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();
}
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<IActionResult> 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, "My message: Please provide an email address.");
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<IActionResult> 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();
        }
    }
}
Login.cshtml
@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>
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("This is a message."); 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论