Author : MD TAREQ HASSAN | Updated : 2020/09/01
EditForm requires a Model parameter, or an EditContext parameter, but not both
Reason: declared model variable in @code{}
or code behind but did not instantiate it
Solution: instantiate model in OnInitialized()
before using for binding i.e. CreateEmailModel = new CreateEmailModel()
public class CreateBase: ComponentBase
{
protected CreateEmailModel CreateEmailModel { get; set; }
protected override void OnInitialized()
{
CreateEmailModel = new CreateEmailModel();
}
protected void OnValidSubmission(EditContext editContext)
{
// ... ... ...
}
}
OnInitialized or OnInitializedAsync called twice
- Blazor server app
Pages/_Host.cshtml
OnInitialized
orOnInitializedAsync
inIndex
page will be called twice- Details: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle#component-initialization-methods
- Work around from Microsoft: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle#stateful-reconnection-after-prerendering
Simple solution 1: use static _firstRender
flag
// ... ... ...
private static bool _firstRender = true;
protected override async Task OnInitializedAsync()
{
if (_firstRender)
{
_firstRender = !_firstRender;
}
else
{
Log.Information("Initializing required data");
try
{
// ... ... ...
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
ErroMessage = "Unexpected error occured!";
}
}
}
// ... ... ...
Simple solution 2: use OnAfterRenderAsync
or OnAfterRender
(will be called only once)
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await ...
}
}
OnAfterRenderAsync
is different than other lifecycle callbacks, see (Note): https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle#after-component-render
NavigateTo is not working
NavigationManager.NavigateTo(...);
is not working (redirection is not happening)- Solution (try and see):
- Check that your delete operation in service class (i.e. ``) eagerly loading navigational properties
- See: Eager loading with Find or FindAsync
- Skip this step if the problem is not related to delete operation
- put/use
NavigationManager.NavigateTo("/foos")
in the component itself
- Check that your delete operation in service class (i.e. ``) eagerly loading navigational properties
Pages/Delete.razor.cs
// ... ... ...
// `ErroMessage` and `NavigationManager` are properties defined in `BaseComponent`
protected async Task PerformDeletion(MouseEventArgs _)
{
ErroMessage = string.Empty;
try
{
var success = await FooService.DeleteFooAsync(Id);
if (success)
{
Log.Warning($"Foo {Id} was deleted");
NavigationManager.NavigateTo("/foos");
}
else
{
ErroMessage = "Failed to Delete Foo";
}
}
catch (AppException ex)
{
ErroMessage = ex.Message;
}
//JSRuntime.InvokeVoidAsync("Interop.RedirectTo", "/foos?deleted=true");
}
// ... ... ...
project assets json related
Possible reason 1: Using Tortoise Git? when cloning, folder name has “%20” & Visual Studio fails to build.
Solution: Replace “%20” with space