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;
    
    您好,谢谢您的提示。我在谷歌上搜索了“引用类型分配”,似乎我的方法没有复制数据,只是创建了一个指向源的地址。这意味着,如果我修改其中一个,它们都将被更改,因为它们指向相同的位置。