英文:
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:
<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
?
答案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 => x.Name == "Name");
if(child != null)
{
<MudTextField @bind-Value=@child.Value></MudTextField>
}
}
@code {
Parent parent = new();
protected override Task OnInitializedAsync()
{
parent.Params = new Child[] { new Child() };
return base.OnInitializedAsync();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论