dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Cannot use EditForm inside of AuthorizeView component #11852

Closed ADefWebserver closed 5 years ago

ADefWebserver commented 5 years ago

Describe the bug

When I try to put an EditForm in an Authorize view I get build errors

To Reproduce

Create a new Server Side Blazor project with preview 6. Put an EditForm in an Authorize view like this:

<AuthorizeView>
    <!-- Show this section if the user is logged in -->
    <Authorized>
        <h4>Hello, @context.User.Identity.Name!</h4>

        <!-- Edit form for the current forecast -->
        <EditForm Model="@objWeatherForecast" OnValidSubmit="@SaveForecast">
            <DataAnnotationsValidator />
            <ValidationSummary />
            <div class="modal-body">
                <input class="form-control" type="text"
                       placeholder="Celsius forecast"
                       @bind="@objWeatherForecast.TemperatureC" />
                <input class="form-control" type="text"
                       placeholder="Fahrenheit forecast"
                       @bind="@objWeatherForecast.TemperatureF" />
                <input class="form-control" type="text"
                       placeholder="Summary"
                       @bind="@objWeatherForecast.Summary" />
                <br />
                <!-- Button to save the forecast -->
                <button class="btn btn-primary"
                        type="submit">
                    Save
                </button>
                <!-- Only show delete button if not a new record -->
                @if (objWeatherForecast.Id > 0)
                {
                    <!-- Button to delete the forecast -->
                    <button class="btn btn-primary"
                            @onclick="@DeleteForecast">
                        Delete
                    </button>
                }
            </div>
        </EditForm>

    </Authorized>
    <!-- Show this section if the user is not logged in -->
    <NotAuthorized>
        <p>You're not signed in.</p>
    </NotAuthorized>
</AuthorizeView>

Expected behavior

That it would build.

Screenshots

image

campersau commented 5 years ago

That's because AuthorizeView and EditForm both use the same Context variable. To avoid this you can change the variable name on them like this:

<AuthorizeView Context="authContext">
    <Authorized>
        <EditForm Context="formContext">
            ...
        </EditForm>
    </Authorized>
</AuthorizeView>

See https://docs.microsoft.com/de-de/aspnet/core/blazor/components?view=aspnetcore-3.0#template-context-parameters

ADefWebserver commented 5 years ago

@campersau Thanks that works!