dotnet / dotnet/aspnetcore

Add mechanism for interactively-rendered form to submit as HTTP request to SSR endpoint

Open
#53,129 8 comments 12 reactions 0 assignees View on GitHub
area-blazor enhancement Pillar: Complete Blazor Web Priority:1
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I need to work with page in interactive mode, and after that submit to handle it with access to HttpContext.

I expect that model will be filled by `[SupplyParameterFromForm] RegisterInputModel Model`

after i submit form with javascript interop and invoke a function `submit()`.

But in Server interactive mode the hidden value `` is removed.

My temporary working soution is:

```cs


@if(HttpContext == null)
{

}

@* form code and markup *@


Login

@code {
[CascadingParameter]
private HttpContext? HttpContext { get; set; }

[SupplyParameterFromForm]
private LoginInputModel Input { get; set; } = new();

[SupplyParameterFromQuery]
private string? ReturnUrl { get; set; }

protected override async Task OnInitializedAsync()
{

if (HttpContext != null && HttpMethods.IsGet(HttpContext.Request.Method))
{
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
}
}

public async Task LoginUser()
{
if(HttpContext == null)
{
// interactive logic
var ref1 = await Js.InvokeAsync("document.getElementById", "login-form");
await ref1.InvokeVoidAsync("parentNode.submit");
return;
}

// server submit logic
var result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
Logger.LogInformation("User logged in.");
RedirectManager.RedirectTo(ReturnUrl);
}
else if (result.RequiresTwoFactor)
{
RedirectManager.RedirectTo(
"Account/LoginWith2fa",
new() { ["returnUrl"] = ReturnUrl, ["rememberMe"] = Input.RememberMe });
}
else if (result.IsLockedOut)
{
Logger.LogWarning("User account locked out.");
RedirectManager.RedirectTo("Account/Lockout");
}else
{
AppStore.State.NotAuthorizedModel = new () { Message = "Other error" };
}
}

}

```

### Describe the solution you'd like

solution 1: Do not remove hidden field `_handler` with `FormName` value and in interactive mode.

solution 2: Provide some API in `EditContext` to call submit with reload page.

### Additional context

Sorry for my English...

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.