Hey,
Last several days i'm totally struggling with selectable list or it call maybe drop-down list (I'm back-end developer). Anyway, can't make it working with drop-down list if drop-down list items is dynamic get data from server.
This is enum drop-down list working great:
<div class="form-group">
<select class="form-control" @bind(_user.UserType)>
@foreach (var userType in Enum.GetValues(typeof(UserType)))
{
<option>@userType</option>
}
</select>
</div>
This one is not building:
<div class="form-group">
<select class="form-control" @bind(_user.CompanyName)> // CompanyName - string
@foreach (var company in _companies) // _companies is List<Company> Company object contain Id and name
{
<option [email protected]>@company.name</option>
}
</select>
</div>
</div>
Also possible that i'm just doing it wrong (I guess it 90% possibility). Can anyone tell what i'm doing wrong?
I tried your code and it works fine for me. Here is my complete sample:
@page "/counter"
<div class="form-group">
<select class="form-control" bind="@_user.CompanyName"> // CompanyName - string
@foreach (var company in _companies) // _companies is List<Company> Company object contain Id and name
{
<option [email protected]>@company.name</option>
}
</select>
<button @onclick(Print)>Print</button>
</div>
@functions {
public class Company
{
public string name { get; set; }
}
public class User
{
public string CompanyName { get; set; }
}
public User _user { get; set; } = new User { CompanyName = "" };
public List<Company> _companies { get; set; } = new List<Company>
{
new Company { name = "Demo" },
new Company { name = "Demo2" }
};
public void Print()
{
Console.WriteLine(_user.CompanyName);
}
}
Does my sample code work for you? Did it help to find the root cause of your problem?

I pasted your code to new blazer project. Very strange, but Console output line where should be company name is empty..
Do you use 0.1.0? I use the new binding syntax -> latest build/NuGet packages. If you use 0.1.0, you have to switch to old @bind. Does that help?
Yes, I have used 0.1.0 version. After switched to old one, yea it now working. So that great, thanks!
I'm getting an odd issue when trying to bind nullable ints. Is this related to the new syntax?
Binding nullable int to input causes null reference exception aspnet/Blazor#466 Look at this ticket
@kieronlanning try binding to a string property instead as a converter e.g.
public int? Value {get;set;}
public string ValueConverter {
get
{
return Value?.value.ToString();
}
set
{
Value = string.IsNullOrWhitespace(value) ? null : Int32.Parse(value);
}
}
@conficient Thanks, what I've ended up doing is something like this (dependent on the property type):
bind=@(object.Property ?? 0)
@kieronlanning that's okay if you only wanted a one-way binding. My converter was intended to work for two-way bindings. Hopefully 0.2.0 will add support for different types.
@conficient Ahhh, I didn't realise.
Thanks!
Closing as the original issue was confirmed as resolved.