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

Databinding in Blazor: Is it possible to use Complex type as a property type on a Component for databinding? #55773

Closed
1 task done
htmlsplash opened this issue May 17, 2024 · 3 comments
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

@htmlsplash
Copy link

htmlsplash commented May 17, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

When writing a custom control in Blazor and then exposing the parameter property for databinding of non-primitive data type (ex. an object with properties), is it possible to databind to that parameter property.

Example (AutoComplete list component property):

` [Parameter]
public AutoCompleteListItem SelectedItem { get; set; }

[Parameter]
public EventCallback SelectedItemChanged { get; set; }`

AutoCompleteListItem definition:

` public class AutoCompleteListItem
{
public string Text { get; set; }
public string Value { get; set; }

}`

Binding to it on a page:

`<AutoCompleteList @rendermode=InteractiveWebAssembly
@bind-SelectedItem.Value="SearchParams.CountyId"
>

@code {

public ProjectSearchForm SearchParams { get; set; } = new();

public class SearchForm
{
	public string CountyId { get; set; }
	public string MunicipalityCode { get; set; }
}

}`

The bind directive is binding to the selected item's Value property which is of type AutoCompleteListItem.

Is this possible in Blazor and if not, workaround or possible support in the future? Or for databinding I have to stick to primitive datatypes such as int, string, double?

  1. Aside question: Notice that the autocomplete list is using render mode: InteractiveWebAssembly. The list is inside an EditForm component that lives on an SSR page. When I submit the page to the server (using Editform), will the value of my autocomplete list bind to the SearchForm.CountyId field on the server? Currently, I've been trying to do that and the field is blank on the server (and I have changed my autocomplete list binding to bind to simple string value).
    Basically, I am trying to use an interactive component on the client that captures users selection and submit this value to the server using traditional SSR form submit. It appears to me that I would have to somehow capture the value in a hidden field that is inside some form and manually extract it on the server? It would be nice if we could use interactive web assembly components and still capture their state on the server using SSR request/response model.

Describe the solution you'd like

See description of the problem.

Additional context

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label May 17, 2024
@htmlsplash htmlsplash changed the title Databinding Binding in Blazor: Is it possible to bind to parameter properties of more Complex type? Databinding in Blazor: Is it possible to use Complex type as a parameter property for databinding? May 17, 2024
@htmlsplash htmlsplash changed the title Databinding in Blazor: Is it possible to use Complex type as a parameter property for databinding? Databinding in Blazor: Is it possible to use Complex type as a parameter property type on a Component for databinding? May 17, 2024
@htmlsplash htmlsplash changed the title Databinding in Blazor: Is it possible to use Complex type as a parameter property type on a Component for databinding? Databinding in Blazor: Is it possible to use Complex type as a property type on a Component for databinding? May 17, 2024
@javiercn
Copy link
Member

@htmlsplash thanks for contacting us.

<AutoCompleteList @rendermode=InteractiveWebAssembly @bind-SelectedItem.Value="SearchParams.CountyId

This will not work because you are crossing the render mode boundary and only primitive types that we can serialize can do so. For what you are trying to achieve, I suspect that you should instead bind the data inside the AutoCompleteList, have a handler to handle the form post/submit. Take different actions whether you are rendering SSR or WebAssembly:

  • SSR: Grab the bound data from the form, and trigger an event so that your parent component can react to the data.
  • WebAssembly: Render the autocomplete list and when the user changes the input/updates the UI, trigger a form post to the server to get a page update.

Alternatively, it might be worth using a datalist element and an input with the list attribute.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels May 21, 2024
@htmlsplash
Copy link
Author

htmlsplash commented May 21, 2024

@javiercn Can you point me to docs on how to the post/submit method?

Please note, that in practice, our form might have many auto-complete lists and I only want to submit the form when the user clicks the search button.

Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

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

3 participants
@javiercn @htmlsplash and others