MudBlazor MudTextField 绑定值失败

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

MudBlazor MudTextField failed to bind value

问题

I am trying to bind a value in MudTextField, but it returns the error CS0131: The left-hand side of an assignment must be a variable, property, or indexer.

Is it a syntax issue which caused the error? Here's some sample code I used, but where I still failed to bind the value:

<MudTextField @bind-Value="parent.Params.FirstOrDefault(x => x.Name == 'Test')?.Value"></MudTextField>
<MudTextField @bind-Value='parent.Params.FirstOrDefault(x => x.Name == "Test")?.Value'></MudTextField>
<MudTextField @bind-Value='@(parent.Params.FirstOrDefault(x => x.Name == "Test")?.Value)'></MudTextField>

These are the models:

public class Parent
{
    public string Id { get; set;}
    public Child[] Params { get; set; }
}
public class Child 
{
    public string Name { get; set; }
    public string Value { get; set; }
}

I can use a different way to bind the value, but the LINQ / one-line-solution looks cleaner and elegant. How can I correctly bind the value to MudTextField?

英文:

I am trying to bind a value in MudTextField, but it returns the error CS0131: The left-hand side of an assignment must be a variable,property or indexer.

Is it a syntax issue which caused the error? Here's some sample code I used, but where I still failed to bind the value:

&lt;MudTextField @bind-Value=&quot;parent.Params.FirstOrDefault(x =&gt; x.Name == &#39;Test&#39;)?.Value&quot;&gt;&lt;/MudTextField&gt;
&lt;MudTextField @bind-Value=&#39;parent.Params.FirstOrDefault(x =&gt; x.Name == &quot;Test&quot;)?.Value&#39;&gt;&lt;/MudTextField&gt;
&lt;MudTextField @bind-Value=&#39;@(parent.Params.FirstOrDefault(x =&gt; x.Name == &quot;Test&quot;)?.Value)&#39;&gt;&lt;/MudTextField&gt;

These are the models:

public class Parent
{
    public string Id { get; set;}
    public Child[] Params { get; set; }
}
public class Child 
{
    public string Name { get; set; }
    public string Value { get; set; }
}

I can use a different way to bind the value, but the LINQ / one-line-solution looks cleaner and elegant. How can I correctly bind the value to MudTextField?

答案1

得分: 0

你不能对null进行双向绑定。让我们想象一下,您编辑了文本字段并将值分配给了'null'。您使用了'?.', 对吧?这有意义吗?'null'不是'变量、属性或索引器'。一个一行的代码不应该是您的目标...

英文:

You cannot do two-way binding to null. Let's imagine you edit the text field and value is assigned to 'null'. You have used '?.', haven't you? Does that even make sense? 'null' is not 'variable,property or indexer'. A one-liner should not be your goal...

@{
    Child child = parent.Params.FirstOrDefault(x =&gt; x.Name == &quot;Name&quot;);
    
    if(child != null)
    {
        &lt;MudTextField @bind-Value=@child.Value&gt;&lt;/MudTextField&gt;
    }
}

@code {
    Parent parent = new();

    protected override Task OnInitializedAsync()
    {
        parent.Params = new Child[] { new Child() };
        return base.OnInitializedAsync();
    }
}

huangapple
  • 本文由 发表于 2023年5月17日 17:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270811.html
匿名

发表评论

匿名网友

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

确定