英文:
Unable to get Default value in MudSelect
问题
以下是翻译好的内容:
<MudSelect T="ModelName" Label="选择姓名" @bind-Value="?????????">
@foreach (var item in LoadNames)
{
<MudSelectItem Value="@(new ModelName{ scid = item.scid, name= item.name })">@item.name</MudSelectItem>
}
</MudSelect>
无法与默认值绑定,
这是默认值:
public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = "xyz" };
如何将默认值与模型绑定。
英文:
<MudSelect T="ModelName" Label="Select Name" @bind-Value="?????????">
@foreach (var item in LoadNames)
{
<MudSelectItem Value="@(new ModelName{ scid = item.scid, name= item.name })">@item.name</MudSelectItem>
}
</MudSelect>
Unable to bind with the default Value,
Here is the Default value
public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = "xyz" };
how to Bind the Default value with the Model
答案1
得分: 1
将@bind-Value
设置为defaultValue
,这是一个可运行的示例:
<MudSelect T="ModelName" Label="Select Name" @bind-Value="@defaultValue">
@foreach (var item in LoadNames)
{
<MudSelectItem Value="@item">@item.name</MudSelectItem>
}
</MudSelect>
在@code
部分:
public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = "xyz" };
private List<ModelName> LoadNames = new List<ModelName>();
protected override void OnInitialized()
{
LoadNames.Add(new ModelName {name="test 1", scid = 1});
LoadNames.Add(new ModelName {name="test 2", scid = 2});
LoadNames.Add(defaultValue);
}
public class ModelName {
public int scid {get;set;}
public string name {get;set;}
}
英文:
You were close, set the @bind-Value
to defaultValue
, here is a working example:
<MudSelect T="ModelName" Label="Select Name" @bind-Value="@defaultValue">
@foreach (var item in LoadNames)
{
<MudSelectItem Value="@item">@item.name</MudSelectItem>
}
</MudSelect>
@code {
public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = "xyz" };
private List<ModelName> LoadNames = new List<ModelName>();
protected override void OnInitialized()
{
LoadNames.Add(new ModelName {name="test 1", scid = 1});
LoadNames.Add(new ModelName {name="test 2", scid = 2});
LoadNames.Add(defaultValue);
}
public class ModelName {
public int scid {get;set;}
public string name {get;set;}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论