无法获取MudSelect中的默认值

huangapple go评论60阅读模式
英文:

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" };

如何将默认值与模型绑定。

英文:
 &lt;MudSelect T=&quot;ModelName&quot; Label=&quot;Select Name&quot; @bind-Value=&quot;?????????&quot;&gt;
           @foreach (var item in LoadNames)
            {
             &lt;MudSelectItem Value=&quot;@(new ModelName{ scid = item.scid, name= item.name })&quot;&gt;@item.name&lt;/MudSelectItem&gt;
            }
 &lt;/MudSelect&gt;

Unable to bind with the default Value,
Here is the Default value

public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = &quot;xyz&quot; };

how to Bind the Default value with the Model

答案1

得分: 1

@bind-Value设置为defaultValue,这是一个可运行的示例

&lt;MudSelect T=&quot;ModelName&quot; Label=&quot;Select Name&quot; @bind-Value=&quot;@defaultValue&quot;&gt;
    @foreach (var item in LoadNames)
    {
        &lt;MudSelectItem Value=&quot;@item&quot;&gt;@item.name&lt;/MudSelectItem&gt;
    }
&lt;/MudSelect&gt;

@code部分:

public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = &quot;xyz&quot; };

private List&lt;ModelName&gt; LoadNames = new List&lt;ModelName&gt;();  

protected override void OnInitialized()
{
    LoadNames.Add(new ModelName {name=&quot;test 1&quot;, scid = 1});
    LoadNames.Add(new ModelName {name=&quot;test 2&quot;, 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:

&lt;MudSelect T=&quot;ModelName&quot; Label=&quot;Select Name&quot; @bind-Value=&quot;@defaultValue&quot;&gt;
    @foreach (var item in LoadNames)
    {
        &lt;MudSelectItem Value=&quot;@item&quot;&gt;@item.name&lt;/MudSelectItem&gt;
    }
 &lt;/MudSelect&gt;

@code {
    public ModelName defaultValue { get; set; } = new ModelName { scid = 101, name = &quot;xyz&quot; };

    private List&lt;ModelName&gt; LoadNames = new List&lt;ModelName&gt;();  

    protected override void OnInitialized()
    {
        LoadNames.Add(new ModelName {name=&quot;test 1&quot;, scid = 1});
        LoadNames.Add(new ModelName {name=&quot;test 2&quot;, scid = 2});
        LoadNames.Add(defaultValue);
    }

    public class ModelName {
        public int scid {get;set;}
        public string name {get;set;}
    }
}

huangapple
  • 本文由 发表于 2023年6月1日 04:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定