我需要将@bind SelectedValues绑定到ductionary中的值。但它给出了CS1503参数2:无法从"Microsoft.AspNetCore.Components.EventCallback<System.Collections.Generic.List>'到"Microsoft.AspNetCore.Components.EventCallback">
<MudTable Items="users.UserList" Loading="@_loading" LoadingProgressColor="Color.Info">
<HeaderContent>
<MudTh>User</MudTh>
<MudTh>Applications</MudTh>
<MudTh>Edit</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="User">@context.FullName</MudTd>
<MudTd DataLabel="Application">
<MudSelect Variant="Variant.Outlined" T="int" Label="Applications" MultiSelection="true"
@bind-Value="value"
@bind-SelectedValues="userAppsDict[context.Id]">
@foreach(var item in apps.ApplicationList)
{
<MudSelectItem T="int" Value="@item.Id">@item.Name</MudSelectItem>
}
</MudSelect>
</MudTd>
<MudTd>
<MudButton Color="Color.Primary" Variant="Variant.Filled">Update</MudButton>
</MudTd>
</RowTemplate>
</MudTable>
@code{
[Inject]
private IApplicationService service{ get; set; }
private int value { get; set; }
private IEnumerable<int> options { get; set; } = new HashSet<int>();
private bool _loading = false;
private Dictionary<int, List<int>> userAppsDict { get; set; } = new Dictionary<int, List<int>>();
UserHasApplicationsReadViewModel userApps = new UserHasApplicationsReadViewModel();
UserReadViewModel users = new UserReadViewModel();
ApplicationReadViewModel apps = new ApplicationReadViewModel();
protected override async Task OnInitializedAsync()
{
_loading = true;
userApps = await service.GetUserHasApplications();
users = await service.GetUsers();
apps = await service.GetApplicationsAsync();
foreach(var user in users.UserList)
{
List<int> appIdList = new List<int>();
var appsbyUid = await service.GetApplicationByUserId(user.Id);
foreach (var item in appsbyUid.ApplicationList)
{
appIdList.Add(item.Id);
}
userAppsDict.Add(user.Id, appIdList);
}
Console.WriteLine(JsonSerializer.Serialize(userAppsDict));
_loading = false;
}
}
我需要将@bind SelectedValues绑定到ductionary 中的值
需求是一个主观术语。。MB希望将一组字符串放入SelectedValues绑定中,因此阻力最小的路径是让它,然后反向翻译,可能是这样的:
@inject ISnackbar Snackbar
<MudTable Items="users" Loading="@_loading" LoadingProgressColor="Color.Info">
<HeaderContent>
<MudTh>User</MudTh>
<MudTh>Applications</MudTh>
<MudTh>Edit</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="User">@context.FullName</MudTd>
<MudTd DataLabel="Application">
<MudSelect Variant="Variant.Outlined" Label="Applications" MultiSelection="true"
@bind-SelectedValues="context.Apps">
@foreach(var item in apps)
{
<MudSelectItem Value="@item.Name">@item.Name</MudSelectItem>
}
</MudSelect>
</MudTd>
<MudTd>
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@(() => UpdateUser(context))">Update</MudButton>
</MudTd>
</RowTemplate>
</MudTable>
@code{
private bool _loading = false;
List<User> users = new List<User>();
List<App> apps = new List<App>();
protected override async Task OnInitializedAsync()
{
_loading = true;
apps = new List<App>{
new(){
Id = 1,
Name = "FirstApp"
},
new(){
Id = 2,
Name = "SecondApp"
}
};
users = new List<User>{
new User{
Id = 1,
FullName = "Firstappuser",
Apps = new HashSet<string>{ apps.First().Name }
},
new User{
Id = 2,
FullName = "Secondappuser",
Apps = new HashSet<string>{ apps.Last().Name }
},
};
_loading = false;
}
class User{
public int Id {get;set;}
public string FullName {get;set;}
public IEnumerable<string> Apps {get;set;}
}
class App{
public int Id {get;set;}
public string Name {get;set;}
}
void UpdateUser(User u){
var newApps = u.Apps.Select(a => apps.First(app => app.Name == a)).ToArray();
Snackbar.Add("Updating user " + u.FullName + " to have apps " + string.Join(",", newApps.Select(app => app.Id.ToString())));
}
}
我不得不发明类来覆盖用例,但它也强调了不必使用给定的类作为模型;可以随意做任何必要的事情来简单地建模。这里,每个用户都有一个IEnumerable<string>
,它将跟踪用户拥有的应用程序,而不是Dictionary<userid, List<app ids>>
,因此1:M用户:应用程序关联在每个用户内部。。当点击更新按钮时,用户会被传递给处理程序,并且有一个将应用程序名称反转回应用程序的过程(newApps
行,然后我字符串将他们的ID加入到一个小吃条中,以证明它能够将应用程序名称反转为应用程序,并获得ID而不是
https://try.mudblazor.com/snippet/cEQGkmwGgvbuyXTd