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 fast-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 fast-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 fast-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 fast-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 fast-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 fast-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 fast-blazor.
Related Issues (20)
- fix: Content not visible in `FluentPopover` HOT 2
- FluentProfileMenu - Page content is resizing each time profile menu is opening HOT 4
- feat(docs): hotkey to jump to search box HOT 3
- Body Not responsive when close navmenu HOT 1
- fix: Clicking content in the DataGrid's empty content throws an exception
- In Fluent Autocomplete control, From keyboard, On tab key, it doesn't go to the close button. HOT 1
- when press the down arrow button at the end of the populated list of Fluent autocomplete control, it continuously pronounce text in input box in NVDA. HOT 1
- For Fluent Dialog, Tooltip is not keyboard accessible, when mouse hover on the control, tooltip is displayed.
- fix: FluentTabs in Virtualize component - An item with the same key has already been added. HOT 3
- Escape keypress does not cancel the last/top-level dialog in case of nested dialog or message box. HOT 4
- fix: memory / reference issues in FluentSelect HOT 6
- In high contrast Aquatic/desert modes, focus indicator is not visible on Fluent Auto complete control HOT 1
- FluentToolTip - Add ChildContent RenderFragment as it's trigger element HOT 1
- Error when running the MAUI Blazor Fluent App (FluentDesignTheme.razor.js file is not moving to the directory .Deploy and run error ,eventhough deploy is configured in the configuration manager). HOT 1
- fix: Autocomplete attribute in Register.razor of .Net 8 FluentUI Blazor template are incorrect resulting in autocompletion of wrong information HOT 2
- fix: Null reference exception when pressing clear button in FluentAutocomplete HOT 2
- fix: Unexpected Expansion of FluentNavMenu on Mobile When Accessing FluentNavGroup Submenus HOT 7
- docs: fix custom timeout toast HOT 1
- feat: add DialogParameter in FluentDialog HOT 1
- change Items property in Data Grid HOT 1
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 fast-blazor.