Comments (7)
This implementation is even better and more easily manipulatable.
<FluentSelect TOption="Country"
Label="Country"
Width="100%;">
<FluentOption Value="null" Selected="@(Input.CountryId is null)" OnSelect="(val) => Input.CountryId = null">--- Select ---</FluentOption>
@foreach (var item in _countries)
{
<FluentOption TOption="Country" Value="@item.CountryId.ToString()" Selected="@(Input.CountryId == item.CountryId)" OnSelect="(val) => Input.CountryId = item.CountryId">@item.Name</FluentOption>
}
</FluentSelect>
from fluentui-blazor.
You can do either this :
<FluentSelect TOption="Country"
Label="Country"
Items="@_countries"
OptionValue="@(p => p.CountryId.ToString())"
OptionText="@(p => p.Name)" >
</FluentSelect>
@code {
public class Country
{
public int? CountryId { get; set; }
public string? Name { get; set; }
}
public class MyModel
{
public int? CountryId { get; set; } // For new MyModels, no country is preselected
/* Other properties */
}
private List<Country> _countries = new List<Country>
{
new Country { CountryId = null, Name="Select a country"},
new Country { CountryId = 1, Name = "USA" },
new Country { CountryId = 2, Name = "Canada" },
new Country { CountryId = 3, Name = "Mexico" }
};
}
Or
<FluentSelect Label="Country">
<FluentOption Value="null">Select a country</FluentOption>
<FluentOption Value="1">USA</FluentOption>
<FluentOption Value="2">Canada</FluentOption>
<FluentOption Value="3">Mexico</FluentOption>
</FluentSelect>
from fluentui-blazor.
@vnbaaij thanks for clarification. But your examples are missing the bind mechanismn. Could you specify how to bind it to MyModel.CountryId
correctly?
from fluentui-blazor.
Ah, that was because there was no bind in your code as well :)
You can just use @bind-Value="@SelectedValue" or @bind-SelectedOption="@SelectedPerson" as shown in the example on the demo site:
@inject DataSource Data
<FluentSelect TOption="Person"
Label="Select a person"
Items="@Data.People.WithVeryLongName()"
Id="people-listbox"
Width="200px"
Height="250px"
OptionValue="@(p => p.PersonId.ToString())"
OptionText="@(p => p.LastName + ", " + p.FirstName)"
@bind-Value="@SelectedValue"
@bind-SelectedOption="@SelectedPerson" />
<p>
Selected value: @SelectedValue <br />
Selected person (strongly typed): @SelectedPerson?.ToString()
</p>
@code
{
Person? SelectedPerson;
string? SelectedValue = "4";
}
from fluentui-blazor.
@vnbaaij my problem here is that I need to bind to the CountryId
of MyModel
I need the Id in integer not as string. Is there any easy way to bind to int directly?
from fluentui-blazor.
You either need to parse it yourself (@bind-Value), or get it from the TOption (@bind-SelectedOption). Like with SelectedPerson.PersonId
in the example above
from fluentui-blazor.
I think I got it. I can use the SelectedOptionChanged
EventCallback
<FluentSelect TOption="Country"
Label="Country"
Width="100%;"
Items="@_countries"
OptionValue="@(p => p.CountryId.ToString())"
OptionText="@(p => p.Name)"
OptionSelected="(option) => Input.CountryId == option.CountryId"
SelectedOptionChanged="(option) => Input.CountryId = option.CountryId > 0 ? option.CountryId : null" />
@code {
public class Country
{
public int CountryId { get; set; }
public string? Name { get; set; }
}
public class MyModel
{
public int? CountryId { get; set; } // For new MyModels, no country is preselected
/* Other properties */
}
private List<Country> _countries = new List<Country>
{
new Country { CountryId = -1, Name="Select a country"},
new Country { CountryId = 1, Name = "USA" },
new Country { CountryId = 2, Name = "Canada" },
new Country { CountryId = 3, Name = "Mexico" }
};
}
Another problem is that the ID itself of the country cannot be null, only from the MyModel itself.
from fluentui-blazor.
Related Issues (20)
- Fluent emoji nuget package increases ram and disk usage to %100 HOT 3
- fix: Rows render multiple times when DataGrid has changes (performance issue) HOT 2
- fix: Column separator can get hidden when resizing columns in DataGrid HOT 4
- fix: OnRowFocus in FluentDataGrid not triggered HOT 1
- Select not working with CultureInfo en-GB on .NET MAUI blazor hybrid HOT 2
- Wizard in Dialog not editable HOT 1
- Export options for data grid HOT 2
- docs: missing clear instructions/example on how to set design tokens application wide HOT 1
- fix: SelectedOptionChanged called twice in FluentCombobox
- feat: add possibility to change field's cursor icon when ReadOnly/Not Editable HOT 1
- Autocomplete with virtualisation shows just a blank list no items are shown in firefox, works fine in chrome and edge browser. HOT 1
- fix: `FluentInputFile` cannot accept dragged files when nested inside a `FluentDropZone` HOT 4
- fix: Icon in default slot will not render correctly in FluentButton.razor if component is wrapped inside another HOT 1
- Add missing values to FluentStack/HorizontalAlignment property type HOT 3
- FluentTree - CurrentSelected does not change if you have an element in the FluentTreeItem HOT 2
- fix: Updating EditContext state in FluentSelect HOT 2
- fix: FluentDataGrid stays in loading state after adding items to the collection HOT 1
- fix: FluentEditForm fields are non editable when wrapped in FluentTab and using version 4.7.2 HOT 4
- feat: add ability to turn off SelectAll button in SelectColumn Multiple mode HOT 2
- fix: initial ActiveIndicator in FluentTabs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluentui-blazor.