I want to access item.Id in @code { } below (while keeping it hidden from view). I don't understand how. Please send help!
I want to avoid using JS as much as possible that is why I am using Blazor in the first place.
I am not great at HTML so maybe there is a better way then using datalist?
Thank you!
{
<input list="listings" id="listing" name="listing" autocomplete="on" placeholder="Search address..." @oninput="HandleInput" @onchange="HandleDatalistOnChange" />
<datalist id="listings">
@foreach (var item in listings)
{
<option id="@item.Id" value="@item.StreetAddress">@item.Municipality, @item.County</option>
}
</datalist>
<button @onclick="GetProperty">Get</button>
}
@code {
private ViewData[] listings;
private string selectedViewData;
protected override async Task OnInitializedAsync()
{
listings = await Http.GetFromJsonAsync<ViewData[]>("https://localhost:44315/api/listings");
}
private void GetProperty()
{
//@TODO get property from db using id
Console.WriteLine(selectedViewData);
}
private async void HandleInput(ChangeEventArgs e)
{
var input = e.Value.ToString();
if (input.Length > 0)
{
listings = await Http.GetFromJsonAsync<ViewData[]>("https://localhost:44315/api/listings/" + input);
StateHasChanged();
await Task.Delay(1);
}
}
private void HandleDatalistOnChange(ChangeEventArgs e)
{
selectedViewData = e.Value.ToString();
Console.WriteLine(selectedViewData);
}
public class ViewData
{
public int Id { get; set; }
public string StreetAddress { get; set; }
public string Municipality { get; set; }
public string County { get; set; }
}
}
idattribute unless using developer tools in the browser. Is that what you're concerned about?@code {}is by displaying it in the value and using onchange to get the value attribute from the option element if that makes sense. but I dont want to put it in the value attribute cause then it is displayed to the user, and the user don't need to know the database id to select property.