Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asp-for taghelper not working properly in DisplayDrivers #15811

Open
Skrypt opened this issue Apr 23, 2024 · 0 comments
Open

asp-for taghelper not working properly in DisplayDrivers #15811

Skrypt opened this issue Apr 23, 2024 · 0 comments
Labels

Comments

@Skrypt
Copy link
Contributor

Skrypt commented Apr 23, 2024

As a follow up to #15488
When a custom validation is required in the UpdateEditorAsync method of a display driver.
The asp-for taghelper doesn't display the proper mutated value.
This can be fixed by updating the updater.ModelState in that method because this is what the asp-for taghelper uses to display a value.
We may find a better fix than updating manually the updater.ModelState.

    public override async Task<IDisplayResult> UpdateAsync(
        CreateSite model,
        ProjectDetails section,
        IUpdateModel updater,
        UpdateEditorContext context
    )
    {
        var viewModel = new ProjectDetailsViewModel();

        await context.Updater.TryUpdateModelAsync(viewModel, Prefix);

        section.ProjectName = viewModel.ProjectName.ToSafeName();

        // This is the fix to get the proper value to display with the asp-for taghelper
        context.Updater.ModelState.SetModelValue(
            $"{Prefix}.{nameof(section.ProjectName)}",
            section.ProjectName,
            section.ProjectName
        );

        // After removing special chars ProjectName could become an empty string.
        if (string.IsNullOrWhiteSpace(section.ProjectName))
        {
            updater.ModelState.AddModelError(Prefix, nameof(section.ProjectName), S["Project name is required"]);
        }
    }

@sebastienros @deanmarcussen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant