I have an Editor with four textbox and two drop-down control. I have binded model class for this Editform. I would like to know how to find whether the model class value has modified or not...
For example
In model class
Field1
Field2
Field3
Field4
Field5
Field6
Active
I would like to set value in "Active" Column as "1" when there is some value is updated in anyone field( field1 or field2 or field3 or field4 or field5 or field6).
Note:-
Field1 to Field6 are bounded from model class in EditForm
ModelClass model = new ModelClass();
public void SavePatient()
{
Console.WriteLine(model);
} |
Thanks for reply...
The above code works only if I give some input to model class (New Entry mode)
When I save data in edit mode without any modification in model class then how can I know the model class value has changed or not .
<EditForm EditContext="@EditContext" OnValidSubmit="@SavePatient">
<DataAnnotationsValidator />
<SfTextBox @bind-Value="@model.textval"></SfTextBox>
<ValidationMessage For="() => model.textval" />
<SfTextBox @bind-Value="@model.textval2"></SfTextBox>
<ValidationMessage For="() => model.textval2" />
<SfTextBox @bind-Value="@model.textval3"></SfTextBox>
<ValidationMessage For="() => model.textval3" />
<SfTextBox @bind-Value="@model.textval4"></SfTextBox>
<ValidationMessage For="() => model.textval4" />
<SfDropDownList TValue="string" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @bind-Value="@model.DropVal">
<DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings>
</SfDropDownList>
<ValidationMessage For="() => model.DropVal" />
<SfDropDownList TValue="string" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @bind-Value="@model.DropVal2">
<DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings>
</SfDropDownList>
<ValidationMessage For="() => model.DropVal2" />
<button type="submit">Submit</button>
</EditForm>
@code{
public EditContext EditContext { get; set; }
public class ModelClass
{
[Required(ErrorMessage = "Value is required")]
public string textval { get; set; }
[Required(ErrorMessage = "Value is required")]
public string textval2 { get; set; }
[Required(ErrorMessage = "Value is required")]
public string textval3 { get; set; }
[Required(ErrorMessage = "Value is required")]
public string textval4 { get; set; }
[Required(ErrorMessage = "Value is required")]
public string DropVal { get; set; }
[Required(ErrorMessage = "Value is required")]
public string DropVal2 { get; set; }
}
ModelClass model = new ModelClass();
public Index()
{
EditContext = new EditContext(model);
}
protected override async Task OnInitializedAsync()
{
EditContext = new EditContext(model);
EditContext.OnFieldChanged += EditContext_OnFieldChanged;
}
protected void EditContext_OnFieldChanged(object sender, FieldChangedEventArgs e)
{
Console.WriteLine($"Field {e.FieldIdentifier.FieldName} has changed");
}
} |
Good..
Thanks...
Hello, what would happen if I revert change to its original value? I am trying to create a functionality which will notify user that you have changes on dialog close and ask if you wish to save them.
How I can do that?
If you revert the changed values back to their original values, the EditContext_OnFieldChanged method will still be triggered because the EditContext will detect the change in the field values. However, the FieldChangedEventArgs object passed to the method will contain the original and reverted values of the field, allowing you to compare them and determine if any actual changes were made.
In your case, you can modify the EditContext_OnFieldChanged method to compare the original and current values of the field and take appropriate action based on the comparison. For example, you can set a flag indicating whether changes have been made and display a notification to the user when they try to close the dialog. Here's an example of how you can modify the method:
protected void EditContext_OnFieldChanged(object sender, FieldChangedEventArgs e) { var fieldIdentifier = e.FieldIdentifier; var fieldName = fieldIdentifier.FieldName; var originalValue = fieldIdentifier.Model.GetType().GetProperty(fieldName)?.GetValue(fieldIdentifier.Model); var currentValue = fieldIdentifier.Model.GetType().GetProperty(fieldName)?.GetValue(fieldIdentifier.Model);
if (!Equals(originalValue, currentValue)) { // Changes have been made Console.WriteLine($"Field {fieldName} has changed");
// Set a flag indicating changes have been made // You can use this flag to display a notification to the user when they try to close the dialog // For example, you can show a confirmation dialog asking if they want to save the changes. } } |
By comparing the original and current values of each field, you can determine if any changes have been made and implement the desired functionality to notify the user and prompt them to save the changes before closing the dialog.
I am trying to check if my form, which contains pre existing data fetched from db, is modified before submitting.
I have given my model class in Editform using
<Editform Model="myModel">
I am then instantiating my editcontext variable in my code as
ModelClass myModel = *get from db*
editContext = new EditContext(myModel)
But when I'm trying to track a change using editContext.IsModified() method, it is returning false even though I modify the fields.
But when, instead of using 'Model' in my EditForm tag, I use <EditForm EditContext="editContext"> i am able to track my changes. How can I do the same using Model="myModel" in my EditForm tag
Hi Utkarsh Agarwal,
Sorry for the delayed response. Regarding the issue you raised about identifying whether the field is modified or not, we want to assure you that it is possible to achieve this using EditContext.IsModified(). To help you better understand the changes, we have attached the updated sample and gif image for your reference. Please get back to us if you need any further assistance.
Regards,
Yohapuja S
I see bug:
var originalValue = fieldIdentifier.Model.GetType().GetProperty(fieldName)?.GetValue(fieldIdentifier.Model);
var currentValue = fieldIdentifier.Model.GetType().GetProperty(fieldName)?.GetValue(fieldIdentifier.Model);
the same data is retrieved twice
Hi Tomasz S
We apologize for any inconvenience caused. We're pleased to inform you that we've addressed the issue "EditContext.IsModified() returns false when changing SfDropDownList value" in our release version 23.1.42. To resolve this issue from your end, we recommend upgrading your package to the latest version.
To provide further assistance and ensure the issue is resolved, we've prepared a sample for your reference:
Sample: https://blazorplayground.syncfusion.com/hZLJNKXJTmgmXxjm
In the above sample, we've disabled the button and enabled it based on the EditContext.IsModified status.
If you encounter any further issues with EditContext.IsModified, kindly follow these steps:
Upgrade your package to version 23.1.42 or later.
If the issue persists, please share a sample with us that demonstrates the problem.
Alternatively, you can modify the previously shared sample to replicate the issue.
Kindly provide us with the replication procedure of the issue, which will help us understand and address the problem effectively.
Once we receive the sample or replication procedure, we'll investigate the issue further to provide you with a solution. Thank you for your cooperation, and we appreciate your patience as we work to resolve this matter.
Regards,
Yohapuja S