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

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

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

// ... ... ...

Possible reason 1: Using Tortoise Git? when cloning, folder name has “%20” & Visual Studio fails to build.
Solution: Replace “%20” with space

Related: https://stackoverflow.com/questions/48440223/assets-file-project-assets-json-not-found-run-a-nuget-package-restore