I'm currently writing a project using Blazor Server (.net 7.0).
So If I have a model with multiple datetime? properties on it, I wish to create a button next to quite a number of datetime field to insert the current date and time which calls a method and updates the property with the current date and time.
Rather than creating a method for every button eg
<button @Onclick="@UpdateNowOn_DateReceived"></Button>
<button @Onclick="@UpdateNowOn_InitialContact"></Button>
protected void UpdateNowOn_DateReceived()
{
Model.Details.DateReceived = DateTime.Now;
StateHasChanged();
}
protected void UpdateNowOn_InitialContact()
{
Model.Details.InitialContact = DateTime.Now;
StateHasChanged();
}
I was hoping I could write a method that I could simply pass the property into an update its value. I'm very new to Blazor and C# so this is all quite new to me (learning as I go).
So I was hoping its possible to do it more like this
<button @Onclick="@UpdateNowOn(Model.Details.DateReceived)"></Button>
<button @Onclick="@UpdateNowOn(Model.Details.InitialContact)"></Button>
protected void UpdateNowOn(DateTime property?) <-- what to pass here
{
Property = DateTime.Now;
StateHasChanged();
}
Any help is appreciated
I've tried to read and use the following but I'm not sure its what I'm after: