NvFieldselect Component - Practical Guide for Blazor Developers
The NvFieldselect component is a Blazor wrapper around Nova's select dropdown component, providing seamless integration with Blazor's data binding, forms, and validation systems. It supports both single and multiple selection modes.
General Blazor Concepts
For general information about data binding, forms, and validation in Nova Blazor components, see the Nova Blazor Integration Guide. This guide focuses on select dropdown-specific features and patterns.
Basic Usage
Simple Select Dropdown
<NvFieldselect Label="Country" Options="countryOptions" />
Select with Description
<NvFieldselect
Label="Priority Level"
Description="Choose the priority for this task"
Options="priorityOptions" />
Two-Way Data Binding
The component supports Blazor's @bind-Value syntax for seamless two-way data binding:
Simple Binding
@code {
private string selectedCountry = "";
private NvFieldselectOptions[] countryOptions = new[]
{
new NvFieldselectOptions { Label = "United States", Value = "us" },
new NvFieldselectOptions { Label = "Canada", Value = "ca" },
new NvFieldselectOptions { Label = "United Kingdom", Value = "uk" },
new NvFieldselectOptions { Label = "Australia", Value = "au" }
};
}
<NvFieldselect
@bind-Value="selectedCountry"
Label="Country"
Options="countryOptions" />
@if (!string.IsNullOrEmpty(selectedCountry))
{
<p>You selected: @selectedCountry</p>
}
Dynamic Options and Binding
@code {
private string selectedCategory = "";
private string selectedItem = "";
private NvFieldselectOptions[] categoryOptions = new[]
{
new NvFieldselectOptions { Label = "Electronics", Value = "electronics" },
new NvFieldselectOptions { Label = "Books", Value = "books" },
new NvFieldselectOptions { Label = "Clothing", Value = "clothing" }
};
private NvFieldselectOptions[] GetItemOptions()
{
return selectedCategory switch
{
"electronics" => new[]
{
new NvFieldselectOptions { Label = "Laptop", Value = "laptop" },
new NvFieldselectOptions { Label = "Phone", Value = "phone" },
new NvFieldselectOptions { Label = "Tablet", Value = "tablet" }
},
"books" => new[]
{
new NvFieldselectOptions { Label = "Fiction", Value = "fiction" },
new NvFieldselectOptions { Label = "Non-Fiction", Value = "non-fiction" },
new NvFieldselectOptions { Label = "Biography", Value = "biography" }
},
"clothing" => new[]
{
new NvFieldselectOptions { Label = "T-Shirt", Value = "tshirt" },
new NvFieldselectOptions { Label = "Jeans", Value = "jeans" },
new NvFieldselectOptions { Label = "Jacket", Value = "jacket" }
},
_ => Array.Empty<NvFieldselectOptions>()
};
}
}
<NvFieldselect
@bind-Value="selectedCategory"
Label="Category"
Options="categoryOptions"
ValueChanged="OnCategoryChanged" />
@if (!string.IsNullOrEmpty(selectedCategory))
{
<NvFieldselect
@bind-Value="selectedItem"
Label="Item"
Options="GetItemOptions()" />
}
@code {
private void OnCategoryChanged(string value)
{
selectedCategory = value;
selectedItem = ""; // Reset item when category changes
}
}
Event Handling
Using ValueChanged Event
<NvFieldselect
@bind-Value="selectedOption"
Label="Choose Option"
Options="optionList"
ValueChanged="OnSelectionChanged" />
@if (!string.IsNullOrEmpty(selectedDetails))
{
<div class="option-details">
<h4>Selection Details</h4>
<p>@selectedDetails</p>
</div>
}
@code {
private string selectedOption = "";
private string selectedDetails = "";
private NvFieldselectOptions[] optionList = new[]
{
new NvFieldselectOptions { Label = "Basic Plan", Value = "basic" },
new NvFieldselectOptions { Label = "Pro Plan", Value = "pro" },
new NvFieldselectOptions { Label = "Enterprise Plan", Value = "enterprise" }
};
private void OnSelectionChanged(string value)
{
selectedOption = value;
selectedDetails = value switch
{
"basic" => "Basic plan includes 5GB storage and email support.",
"pro" => "Pro plan includes 50GB storage, priority support, and advanced features.",
"enterprise" => "Enterprise plan includes unlimited storage, dedicated support, and custom features.",
_ => ""
};
}
}
Event with Binding
@code {
private string selectedTimezone = "utc";
private void OnTimezoneChanged(string value)
{
selectedTimezone = value;
// Could trigger time conversion, save preferences, etc.
UpdateCurrentTime();
}
private void UpdateCurrentTime()
{
// Update time display based on selected timezone
}
}
<NvFieldselect
@bind-Value="selectedTimezone"
Label="Timezone"
Options="timezoneOptions"
ValueChanged="OnTimezoneChanged" />
<p>Current time in selected timezone: @currentTimeDisplay</p>
@code {
private NvFieldselectOptions[] timezoneOptions = new[]
{
new NvFieldselectOptions { Label = "UTC", Value = "utc" },
new NvFieldselectOptions { Label = "EST (UTC-5)", Value = "est" },
new NvFieldselectOptions { Label = "PST (UTC-8)", Value = "pst" },
new NvFieldselectOptions { Label = "CET (UTC+1)", Value = "cet" },
new NvFieldselectOptions { Label = "JST (UTC+9)", Value = "jst" }
};
}
Form Integration
The component integrates seamlessly with Blazor's EditForm for validation and form submission:
Basic Form Integration
<EditForm Model="userProfile" OnValidSubmit="SaveProfile">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="userProfile.FirstName"
Label="First Name"
For="() => userProfile.FirstName" />
<NvFieldtext
@bind-Value="userProfile.LastName"
Label="Last Name"
For="() => userProfile.LastName" />
<NvFieldselect
@bind-Value="userProfile.Country"
Label="Country"
Options="countryOptions"
For="() => userProfile.Country" />
<NvFieldselect
@bind-Value="userProfile.PreferredLanguage"
Label="Preferred Language"
Options="languageOptions"
For="() => userProfile.PreferredLanguage" />
<button type="submit">Save Profile</button>
</EditForm>
@code {
private UserProfile userProfile = new();
private void SaveProfile()
{
// Process profile save
Console.WriteLine("Profile saved successfully!");
}
public class UserProfile
{
[Required]
public string? FirstName { get; set; }
[Required]
public string? LastName { get; set; }
[Required(ErrorMessage = "Please select a country")]
public string? Country { get; set; }
[Required(ErrorMessage = "Please select a preferred language")]
public string? PreferredLanguage { get; set; }
}
private NvFieldselectOptions[] countryOptions = new[]
{
new NvFieldselectOptions { Label = "Select Country", Value = "" },
new NvFieldselectOptions { Label = "United States", Value = "us" },
new NvFieldselectOptions { Label = "Canada", Value = "ca" },
new NvFieldselectOptions { Label = "United Kingdom", Value = "uk" }
};
private NvFieldselectOptions[] languageOptions = new[]
{
new NvFieldselectOptions { Label = "Select Language", Value = "" },
new NvFieldselectOptions { Label = "English", Value = "en" },
new NvFieldselectOptions { Label = "Spanish", Value = "es" },
new NvFieldselectOptions { Label = "French", Value = "fr" },
new NvFieldselectOptions { Label = "German", Value = "de" }
};
}
Validation
Required Field Validation
<EditForm Model="shippingInfo" OnValidSubmit="ProcessOrder">
<DataAnnotationsValidator />
<NvFieldselect
@bind-Value="shippingInfo.ShippingMethod"
Label="Shipping Method"
Required="true"
Options="shippingOptions"
For="() => shippingInfo.ShippingMethod" />
<NvFieldselect
@bind-Value="shippingInfo.PaymentMethod"
Label="Payment Method"
Required="true"
Options="paymentOptions"
For="() => shippingInfo.PaymentMethod" />
<button type="submit">Place Order</button>
</EditForm>
@code {
private ShippingInfo shippingInfo = new();
public class ShippingInfo
{
[Required(ErrorMessage = "Please select a shipping method")]
public string? ShippingMethod { get; set; }
[Required(ErrorMessage = "Please select a payment method")]
public string? PaymentMethod { get; set; }
}
private NvFieldselectOptions[] shippingOptions = new[]
{
new NvFieldselectOptions { Label = "Standard Shipping", Value = "standard" },
new NvFieldselectOptions { Label = "Express Shipping", Value = "express" },
new NvFieldselectOptions { Label = "Overnight Shipping", Value = "overnight" }
};
private NvFieldselectOptions[] paymentOptions = new[]
{
new NvFieldselectOptions { Label = "Credit Card", Value = "credit" },
new NvFieldselectOptions { Label = "PayPal", Value = "paypal" },
new NvFieldselectOptions { Label = "Bank Transfer", Value = "bank" }
};
}
Custom Validation
<EditForm Model="subscriptionModel" OnValidSubmit="Subscribe">
<DataAnnotationsValidator />
<NvFieldselect
@bind-Value="subscriptionModel.Plan"
Label="Subscription Plan"
Options="planOptions"
For="() => subscriptionModel.Plan" />
<NvFieldselect
@bind-Value="subscriptionModel.BillingCycle"
Label="Billing Cycle"
Options="billingOptions"
For="() => subscriptionModel.BillingCycle" />
<button type="submit">Subscribe</button>
</EditForm>
@code {
private SubscriptionModel subscriptionModel = new();
public class SubscriptionModel
{
[Required]
[SubscriptionValidation]
public string? Plan { get; set; }
[Required]
[SubscriptionValidation]
public string? BillingCycle { get; set; }
}
public class SubscriptionValidationAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var subscription = (SubscriptionModel)validationContext.ObjectInstance;
var selectedValue = value as string;
if (string.IsNullOrEmpty(selectedValue))
return new ValidationResult("This field is required");
// Business rule: Enterprise plan requires annual billing
if (subscription.Plan == "enterprise" && subscription.BillingCycle == "monthly")
return new ValidationResult("Enterprise plan requires annual billing");
return ValidationResult.Success;
}
}
private NvFieldselectOptions[] planOptions = new[]
{
new NvFieldselectOptions { Label = "Basic", Value = "basic" },
new NvFieldselectOptions { Label = "Pro", Value = "pro" },
new NvFieldselectOptions { Label = "Enterprise", Value = "enterprise" }
};
private NvFieldselectOptions[] billingOptions = new[]
{
new NvFieldselectOptions { Label = "Monthly", Value = "monthly" },
new NvFieldselectOptions { Label = "Annual", Value = "annual" }
};
}
Advanced Scenarios
Cascading Selects
<NvFieldselect
@bind-Value="selectedCountry"
Label="Country"
Options="countryOptions"
ValueChanged="OnCountryChanged" />
@if (!string.IsNullOrEmpty(selectedCountry))
{
<NvFieldselect
@bind-Value="selectedState"
Label="State/Province"
Options="GetStateOptions()"
ValueChanged="OnStateChanged" />
}
@if (!string.IsNullOrEmpty(selectedState))
{
<NvFieldselect
@bind-Value="selectedCity"
Label="City"
Options="GetCityOptions()" />
}
@code {
private string selectedCountry = "";
private string selectedState = "";
private string selectedCity = "";
private void OnCountryChanged(string value)
{
selectedCountry = value;
selectedState = "";
selectedCity = "";
}
private void OnStateChanged(string value)
{
selectedState = value;
selectedCity = "";
}
private NvFieldselectOptions[] GetStateOptions()
{
return selectedCountry switch
{
"us" => new[]
{
new NvFieldselectOptions { Label = "California", Value = "ca" },
new NvFieldselectOptions { Label = "New York", Value = "ny" },
new NvFieldselectOptions { Label = "Texas", Value = "tx" }
},
"ca" => new[]
{
new NvFieldselectOptions { Label = "Ontario", Value = "on" },
new NvFieldselectOptions { Label = "Quebec", Value = "qc" },
new NvFieldselectOptions { Label = "British Columbia", Value = "bc" }
},
_ => Array.Empty<NvFieldselectOptions>()
};
}
private NvFieldselectOptions[] GetCityOptions()
{
return selectedState switch
{
"ca" => new[]
{
new NvFieldselectOptions { Label = "Los Angeles", Value = "la" },
new NvFieldselectOptions { Label = "San Francisco", Value = "sf" },
new NvFieldselectOptions { Label = "San Diego", Value = "sd" }
},
"ny" => new[]
{
new NvFieldselectOptions { Label = "New York City", Value = "nyc" },
new NvFieldselectOptions { Label = "Buffalo", Value = "buf" },
new NvFieldselectOptions { Label = "Albany", Value = "alb" }
},
_ => Array.Empty<NvFieldselectOptions>()
};
}
}
Options with Disabled Items
<NvFieldselect
@bind-Value="selectedPlan"
Label="Choose Plan"
Options="planOptions" />
@code {
private string selectedPlan = "";
private NvFieldselectOptions[] planOptions = new[]
{
new NvFieldselectOptions { Label = "Free Plan", Value = "free" },
new NvFieldselectOptions { Label = "Basic Plan - $9/month", Value = "basic" },
new NvFieldselectOptions { Label = "Pro Plan - $29/month", Value = "pro", Disabled = true },
new NvFieldselectOptions { Label = "Enterprise Plan - Contact Sales", Value = "enterprise", Disabled = true }
};
}
Readonly Mode with Display Value
<NvFieldselect
@bind-Value="confirmedSelection"
Label="Confirmed Selection"
Options="statusOptions"
Readonly="true"
DisplayValue="true" />
@code {
private string confirmedSelection = "approved";
private NvFieldselectOptions[] statusOptions = new[]
{
new NvFieldselectOptions { Label = "Pending", Value = "pending" },
new NvFieldselectOptions { Label = "Approved", Value = "approved" },
new NvFieldselectOptions { Label = "Rejected", Value = "rejected" }
};
}
Survey Form with Select Questions
<EditForm Model="surveyResponse" OnValidSubmit="SubmitSurvey">
<DataAnnotationsValidator />
<div class="survey-question">
<NvFieldselect
@bind-Value="surveyResponse.AgeGroup"
Label="What is your age group?"
Options="ageGroupOptions"
For="() => surveyResponse.AgeGroup" />
</div>
<div class="survey-question">
<NvFieldselect
@bind-Value="surveyResponse.ExperienceLevel"
Label="What is your experience level?"
Options="experienceOptions"
For="() => surveyResponse.ExperienceLevel" />
</div>
<div class="survey-question">
<NvFieldselect
@bind-Value="surveyResponse.Satisfaction"
Label="How satisfied are you with our service?"
Options="satisfactionOptions"
For="() => surveyResponse.Satisfaction" />
</div>
<button type="submit">Submit Survey</button>
</EditForm>
@code {
private SurveyResponse surveyResponse = new();
public class SurveyResponse
{
[Required]
public string? AgeGroup { get; set; }
[Required]
public string? ExperienceLevel { get; set; }
[Required]
public string? Satisfaction { get; set; }
}
private NvFieldselectOptions[] ageGroupOptions = new[]
{
new NvFieldselectOptions { Label = "18-24", Value = "18-24" },
new NvFieldselectOptions { Label = "25-34", Value = "25-34" },
new NvFieldselectOptions { Label = "35-44", Value = "35-44" },
new NvFieldselectOptions { Label = "45-54", Value = "45-54" },
new NvFieldselectOptions { Label = "55+", Value = "55+" }
};
private NvFieldselectOptions[] experienceOptions = new[]
{
new NvFieldselectOptions { Label = "Beginner", Value = "beginner" },
new NvFieldselectOptions { Label = "Intermediate", Value = "intermediate" },
new NvFieldselectOptions { Label = "Advanced", Value = "advanced" },
new NvFieldselectOptions { Label = "Expert", Value = "expert" }
};
private NvFieldselectOptions[] satisfactionOptions = new[]
{
new NvFieldselectOptions { Label = "Very Dissatisfied", Value = "1" },
new NvFieldselectOptions { Label = "Dissatisfied", Value = "2" },
new NvFieldselectOptions { Label = "Neutral", Value = "3" },
new NvFieldselectOptions { Label = "Satisfied", Value = "4" },
new NvFieldselectOptions { Label = "Very Satisfied", Value = "5" }
};
}
Best Practices
- Always provide a default/placeholder option for better UX (e.g., "Select an option")
- Use clear, descriptive labels for both the select and options
- Order options logically (alphabetical, priority-based, or frequency-based)
- Consider using disabled options to show unavailable choices
- Implement proper validation for required selections
- Use cascading selects for hierarchical data to reduce cognitive load
- Consider the number of options - too many may require search functionality
- Group related options using optgroup when appropriate
- Handle dynamic options carefully to avoid disrupting user selections
- Test keyboard navigation and screen reader compatibility
Common Patterns
Country Selection
<EditForm Model="addressModel" OnValidSubmit="SaveAddress">
<DataAnnotationsValidator />
<NvFieldselect
@bind-Value="addressModel.Country"
Label="Country"
Options="countryOptions"
For="() => addressModel.Country" />
<NvFieldtext
@bind-Value="addressModel.AddressLine1"
Label="Address Line 1"
For="() => addressModel.AddressLine1" />
<NvFieldtext
@bind-Value="addressModel.City"
Label="City"
For="() => addressModel.City" />
<NvFieldtext
@bind-Value="addressModel.PostalCode"
Label="Postal Code"
For="() => addressModel.PostalCode" />
<button type="submit">Save Address</button>
</EditForm>
@code {
private AddressModel addressModel = new();
public class AddressModel
{
[Required]
public string? Country { get; set; }
[Required]
public string? AddressLine1 { get; set; }
[Required]
public string? City { get; set; }
[Required]
public string? PostalCode { get; set; }
}
private NvFieldselectOptions[] countryOptions = new[]
{
new NvFieldselectOptions { Label = "Select Country", Value = "" },
new NvFieldselectOptions { Label = "United States", Value = "US" },
new NvFieldselectOptions { Label = "Canada", Value = "CA" },
new NvFieldselectOptions { Label = "United Kingdom", Value = "UK" },
new NvFieldselectOptions { Label = "Australia", Value = "AU" },
new NvFieldselectOptions { Label = "Germany", Value = "DE" },
new NvFieldselectOptions { Label = "France", Value = "FR" }
};
}
Settings Panel
<EditForm Model="userSettings" OnValidSubmit="SaveSettings">
<DataAnnotationsValidator />
<NvFieldselect
@bind-Value="userSettings.Theme"
Label="Theme"
Options="themeOptions"
For="() => userSettings.Theme" />
<NvFieldselect
@bind-Value="userSettings.Language"
Label="Language"
Options="languageOptions"
For="() => userSettings.Language" />
<NvFieldselect
@bind-Value="userSettings.Timezone"
Label="Timezone"
Options="timezoneOptions"
For="() => userSettings.Timezone" />
<NvFieldselect
@bind-Value="userSettings.Notifications"
Label="Notification Preferences"
Options="notificationOptions"
For="() => userSettings.Notifications" />
<button type="submit">Save Settings</button>
</EditForm>
@code {
private UserSettings userSettings = new() { Theme = "light", Language = "en" };
public class UserSettings
{
[Required]
public string? Theme { get; set; }
[Required]
public string? Language { get; set; }
public string? Timezone { get; set; }
public string? Notifications { get; set; }
}
private NvFieldselectOptions[] themeOptions = new[]
{
new NvFieldselectOptions { Label = "Light", Value = "light" },
new NvFieldselectOptions { Label = "Dark", Value = "dark" },
new NvFieldselectOptions { Label = "Auto", Value = "auto" }
};
private NvFieldselectOptions[] languageOptions = new[]
{
new NvFieldselectOptions { Label = "English", Value = "en" },
new NvFieldselectOptions { Label = "Spanish", Value = "es" },
new NvFieldselectOptions { Label = "French", Value = "fr" },
new NvFieldselectOptions { Label = "German", Value = "de" }
};
}
This guide covers the most common use cases and patterns for the NvFieldselect component in Blazor applications. For more advanced scenarios or specific requirements, refer to the component's API documentation or the Storybook examples.