I am developing a website in Blazor 8.0 (Static Server Side Rendering) and I want to programmatically bind my model to an EditForm and its input fields. My goal is to avoid using 'magic strings' completely throughout my project.
In order to derive the full name of a particular property I have after some searching come up with the following method:
public static string FullNameOf<T>(Expression<Func<T>> memberExpression) { if (memberExpression.Body is not MemberExpression obj) return string.Empty; var result = obj.Member.Name; while (obj.Expression is MemberExpression obj2) { result = obj2.Member.Name +"." + result; obj = obj2; } return result; } Which I then use along with nameof and the existing bindings in Blazor.
<EditForm OnValidSubmit="OnValidSubmit" Model="CreateFileModel" FormName="@nameof(CreateFileModel)" enctype="multipart/form-data">...<InputFile name="@FullNameOf(() => CreateFileModel.File)" type="file" /></EditForm>This is so far the best solution I've come up with. Am I missing anything? I find it particularly strange that InputFile is missing the ability to automatically bind to the name of a property of a model. It must be that I missed a key piece of information online.
I was expecting a functionality of some kind to exist like this:
<InputFile Name="CreateFileModel.File" ... />