Asp.net core Blazor中相同类型的属性值更新不正确
背景:Asp.net core Blazor中相同类型的属性值更新不正确,asp.net-core,components,blazor,Asp.net Core,Components,Blazor,背景: **index.razor** @page "/" @using SolutionName.Data @using System.Reflection <EditForm Model="Items2"> <table class="table"> <thead> <tr> <th>Sum
**index.razor**
@page "/"
@using SolutionName.Data
@using System.Reflection
<EditForm Model="Items2">
<table class="table">
<thead>
<tr>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var i in Items2)
{
<tr @key="@i.GetHashCode()">
<InputText @bind-Value="i.Summary"></InputText>
</tr>
}
</tbody>
</table>
</EditForm>
//
//reflections for debuggings
//
@if (Items != null)
{
<p>
@foreach (var item in Items)
{
<span>@($"Items.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@if (Items2 != null)
{
<p>
@foreach (var item in Items2)
{
<span>@($"Items2.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@code{
List<WeatherForecast> Items = new List<WeatherForecast>();
List<WeatherForecast> Items2 = new List<WeatherForecast>();
protected override void OnInitialized()
{
Items = new List<WeatherForecast>()
{
new WeatherForecast()
{
Date = DateTime.Now,
Summary = "123",
TemperatureC = 1
}
};
Items2 = Items;
}
private void ResetItems2()
{
Items2 = Items;
}
}
我希望实现以下目标:
copyValue=unchangedValue
**index.razor**
@page "/"
@using SolutionName.Data
@using System.Reflection
<EditForm Model="Items2">
<table class="table">
<thead>
<tr>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var i in Items2)
{
<tr @key="@i.GetHashCode()">
<InputText @bind-Value="i.Summary"></InputText>
</tr>
}
</tbody>
</table>
</EditForm>
//
//reflections for debuggings
//
@if (Items != null)
{
<p>
@foreach (var item in Items)
{
<span>@($"Items.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@if (Items2 != null)
{
<p>
@foreach (var item in Items2)
{
<span>@($"Items2.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@code{
List<WeatherForecast> Items = new List<WeatherForecast>();
List<WeatherForecast> Items2 = new List<WeatherForecast>();
protected override void OnInitialized()
{
Items = new List<WeatherForecast>()
{
new WeatherForecast()
{
Date = DateTime.Now,
Summary = "123",
TemperatureC = 1
}
};
Items2 = Items;
}
private void ResetItems2()
{
Items2 = Items;
}
}
**index.razor**
@第“/”页
@使用SolutionName.Data
@使用系统反射
总结
@foreach(项目2中的变量i)
{
}
//
//调试的思考
//
@如果(项!=null)
{
@foreach(项目中的var项目)
{
@($“Items.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast.GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}”)
}
}
@if(Items2!=null)
{
@foreach(项目2中的var项目)
{
@($“Items2.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast.GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}”)
}
}
@代码{
列表项=新列表();
List Items2=新列表();
受保护的覆盖无效OnInitialized()
{
Items=新列表()
{
新天气预报
{
日期=日期时间。现在,
Summary=“123”,
温度c=1
}
};
项目2=项目;
}
私有void ResetItems2()
{
项目2=项目;
}
}
如您所见,我正在将Items2
,而不是项
,绑定到
。
但是,更新摘要似乎同时更新了Items2
和Items
。我还注意到,如果项
和项2
属于两种不同的类型,则不会发生这种情况(例如,它们具有完全相同的属性,我将一个转换为另一个…)
两个问题:
**index.razor**
@page "/"
@using SolutionName.Data
@using System.Reflection
<EditForm Model="Items2">
<table class="table">
<thead>
<tr>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var i in Items2)
{
<tr @key="@i.GetHashCode()">
<InputText @bind-Value="i.Summary"></InputText>
</tr>
}
</tbody>
</table>
</EditForm>
//
//reflections for debuggings
//
@if (Items != null)
{
<p>
@foreach (var item in Items)
{
<span>@($"Items.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@if (Items2 != null)
{
<p>
@foreach (var item in Items2)
{
<span>@($"Items2.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@code{
List<WeatherForecast> Items = new List<WeatherForecast>();
List<WeatherForecast> Items2 = new List<WeatherForecast>();
protected override void OnInitialized()
{
Items = new List<WeatherForecast>()
{
new WeatherForecast()
{
Date = DateTime.Now,
Summary = "123",
TemperatureC = 1
}
};
Items2 = Items;
}
private void ResetItems2()
{
Items2 = Items;
}
}
在这种情况下,项
为什么要更新
有没有办法只更新项目2
而不更新项目
,同时允许项目
和项目2
为同一类型
重现问题的详细步骤:
**index.razor**
@page "/"
@using SolutionName.Data
@using System.Reflection
<EditForm Model="Items2">
<table class="table">
<thead>
<tr>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var i in Items2)
{
<tr @key="@i.GetHashCode()">
<InputText @bind-Value="i.Summary"></InputText>
</tr>
}
</tbody>
</table>
</EditForm>
//
//reflections for debuggings
//
@if (Items != null)
{
<p>
@foreach (var item in Items)
{
<span>@($"Items.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@if (Items2 != null)
{
<p>
@foreach (var item in Items2)
{
<span>@($"Items2.{typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).Name}={typeof(WeatherForecast).GetProperty(nameof(WeatherForecast.Summary)).GetValue(item)}")</span>
}
</p>
}
@code{
List<WeatherForecast> Items = new List<WeatherForecast>();
List<WeatherForecast> Items2 = new List<WeatherForecast>();
protected override void OnInitialized()
{
Items = new List<WeatherForecast>()
{
new WeatherForecast()
{
Date = DateTime.Now,
Summary = "123",
TemperatureC = 1
}
};
Items2 = Items;
}
private void ResetItems2()
{
Items2 = Items;
}
}
第一步。初始化并首次渲染
第二步。将该值更改为456,然后用tab键移开
预期结果应为
项目汇总=123(不是456)
Items2.Summary=456问题在于您正在使用引用类型分配。当您将
项目
分配给项目2
时,实际上是分配了一个指向项目
值的指针。两个变量都指向相同的对象列表
如果适用,则改为创建值类型。将数据保存在本地存储器中,然后检索数据是一个可行的解决方案
这:
List Items=newlist();
List Items2=新列表();
这是多余的。代码如下:
List<WeatherForecast> Items;
List<WeatherForecast> Items2;
列表项;
列出项目2;
您好,谢谢您的提示。我在谷歌上搜索了“引用类型分配”,似乎我的方法没有复制数据,只是创建了一个指向源的地址。这意味着,如果我修改其中一个,它们都将被更改,因为它们指向相同的位置。