Skip to content

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

Closed
@anandgothe

Description

@anandgothe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions