Blazor在一个字段中设置DateTime值会更新另一个字段中的DateTime值
我有两个DateTime字段的编辑形式。。它们是基于模型创建的。我的目标是,如果更改Blazor在一个字段中设置DateTime值会更新另一个字段中的DateTime值,datetime,asp.net-core,razor,blazor,Datetime,Asp.net Core,Razor,Blazor,我有两个DateTime字段的编辑形式。。它们是基于模型创建的。我的目标是,如果更改DateStarted字段的DateTime,它将更新DateStartedCopy字段的值,并根据DateStarted字段中选择的内容动态更新。。但我仍然希望用户能够覆盖该选项,并在需要时选择自己的日期时间 <p> <label for="datestarted">1.Date Started<span style=&q
DateStarted
字段的DateTime,它将更新DateStartedCopy
字段的值,并根据DateStarted
字段中选择的内容动态更新。。但我仍然希望用户能够覆盖该选项,并在需要时选择自己的日期时间
<p>
<label for="datestarted">1.Date Started<span style="color: red">*</span> </label><br />
<InputDate class="inputFieldSize" id="datestarted" @bind-Value="_supervisorData.DateStarted" />
</p>
<p>
<label for="datestartedcopy">2.Date Started Copy<span style="color: red">*</span> </label> <br />
<InputDate class="inputFieldSize" id="datestartedcopy" @bind-Value="_supervisorData.DateStartedCopy" />
</p>
然后是我的实际型号主管数据
[Required]
public DateTime DateStarted { get; set; }
[Required]
public DateTime DateStartedCopy{ get; set; }
我认为一种方法可能是改变我的getter;setter方法..设置DateStartedCopy=DateStarted的值,但我相信我的选择可能是在表单中执行onchange
事件?在这里我应该采取什么样的方法
<input type="date" class="inputFieldSize" id="datestarted"
@bind-value="_supervisorData.DateStarted"
@bind-value:event="oninput"
@onchange="()=> { this._supervisorData.DateStartedCopy = this._supervisorData.DateStarted; }" />
这是可行的,但它使用输入类型=“日期”而不是InputDate,因此您可以定义@bind value:event=“oninput”
当我尝试只添加@onchange时,除了InputDate中的@bind值之外,不会调用onchange。
当我尝试在InputDate内使用@oninput时,@oninput在更改_supervisorData.DateStarted之前被调用,因此您的值是错误的
将“this.\u supervisorData.DateStartedCopy=this.\u supervisorData.DateStarted;”添加到您提到的setter中同样有效。谢谢。。您使用input type=“date”的第一种方法确实部分有效。但问题是,现在当我在
DateStartedCopy
中更改日期时,它也会更新DateStarted
,而这正是我想要的DateStartedCopy
更新DateStartedCopy
但是DateStartedCopy
仍然可以使用自己的日期覆盖,而不会影响原始版本。我不确定您的意思。如果我运行代码并更改DateStartedCopy,则DateStarted不会更改。这就是你想要它工作的方式,不是吗?当DateStartedCopy更改时,没有代码可以更改DateStarted。这很奇怪。。我想知道当我更改DateStartedCopy
时,为什么我的DateStarted
会被更改,你的DateStarted
代码是什么样子?这也是一个输入类型=“日期”或类似于我的原始输入我发布的是DateStart输入的代码。DateStartedCopy输入与您在原始代码中使用的相同
<input type="date" class="inputFieldSize" id="datestarted"
@bind-value="_supervisorData.DateStarted"
@bind-value:event="oninput"
@onchange="()=> { this._supervisorData.DateStartedCopy = this._supervisorData.DateStarted; }" />