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

TryParseValueFromString not firing for component inherited from InputBase<T> in SSR mode #60212

Closed
1 task done
anandgothe opened this issue Feb 5, 2025 · 2 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@anandgothe
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

This is about using Blazor in SSR mode.

When creating a custom razor component that inherits from InputBase class, we are forced to implement the TryParseValueFromString function, since it is declared as abstract in the base class. Which is not a problem, but the TryParseValueFromString function never seem to be triggered. From code in InputBase it appears that TryParseValueFromString should be triggered when value is set to CurrentValueAsString. Hence in my component, I've bound to CurrentValueAsString, but that doesn't help. The companion method FormatValueAsString does get fired then the page is being rendered. But when the page is submitted, the TryParseValueFromString does not fire.

Elsewhere, people have suggested that @Bind=CurrentValue instead of CurrentValueAsString. That does not work either. With CurrentValue we even lose the call to FormatValueAsString.

I want to create an input component where both FormatValueAsString and TryParseValueFromString are triggered so that I can intercept the value on both ends (when value goes out to the browser, and when value comes back from the browser) and make some adjustments.

Expected Behavior

I would expect the TryParseValueFromString to be fired somewhere in the life-cycle, preferable before the razor page init, so that the Model.Greetings has a value that has been parse by our TryParseValueFromString function.

Steps To Reproduce

I have created a simple input component that inherits from InputBase. In this silly example, I want text value to always end with exclamation symbol.

@using System.Diagnostics.CodeAnalysis
@inherits InputBase<string?>

<input type="text" name=@NameAttributeValue @attributes=AdditionalAttributes class=@CssClass @bind=CurrentValueAsString />

@code {
	protected override string? FormatValueAsString(string? value)
	{
		if(!value!.EndsWith("!")) {
			value = value + "!";
		}

		return base.FormatValueAsString(value);
	}

	protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out string? result, [NotNullWhen(false)] out string? validationErrorMessage)
	{
		if (!value!.EndsWith("!"))
		{
			value = value + "!";
		}

		result = value;
		validationErrorMessage = "";
		return true;
	}
}

On my razor page, this is how I used the new component:

@page "/"
Welcome to your new app.

<EditForm Model="Model" FormName="HelloForm">
	<MyInput @bind-Value=Model.Greetings />
	<input type="submit" />
</EditForm>
@code {

	internal class ViewModel {
		internal string Greetings { get; set; } = "Hello";
	}

	internal ViewModel Model = new();

	protected override void OnInitialized()
	{
		// when form is submitted, a breakpoint here always hits, 
		// and Model.Greetings value is always what is entered in the UI, the TryParseValueFromString does not get called, which would have added an exclaimation
		base.OnInitialized();
	}
}

When the form is rendered, the FormatValueAsString is fired as expected. But when the form is submitted, the TryParseValueFromString does not fire.

Sample code available here: https://github.com/anandgothe/SampleForTryParseValueFromString

Exceptions (if any)

No response

.NET Version

9.0.102

Anything else?

Sample code: https://github.com/anandgothe/SampleForTryParseValueFromString

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Feb 5, 2025
@javiercn
Copy link
Member

javiercn commented Feb 6, 2025

@anandgothe thanks for contacting us.

Binding doesn't work that way in SSR mode. You need to use [SupplyParameterFromForm] and declare a public property to bind the data in the component.

At that point is the modelbinding system the one that will map the value from the form data into the model, and no code within MyInput will run at that point.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Feb 6, 2025
@anandgothe
Copy link
Author

@javiercn , thanks for answering. Is there any way I could get my code to intercept this binding process? Basically, I want to preprocess the value coming in from browser before it gets assigned to the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants