I have a SfDataform in a Maui App and I'm using a behavior to validate (manual commit) the data to the dataObject and if valid I use dataForm.Commit() to commit the data to the data object.
I'd then like to go to the view model to save the committed data to the database. I cannot figure out how to do this. I have a dataStore injected in the view model for handling database operations. Can you point me in the right direction?
I am basing my code off this article:
How to save or cancel the edited DataForm data in .NET MAUI DataForm (SfDataForm)?
Hi Carl,
Thank you for reaching out to us. To achieve your requirement, you can refer to our Knowledge Base article, which provides a detailed guide on binding the database to your ViewModel.
If you need further clarification or have any additional questions, please feel free to reach out. We're here to assist you!
Regards,
Vidyalakshmi M.
Thank you for your response but...
I don't have an issue binding data to the view model. I have a save button in my view that has an attached behavior. When the save button is clicked, the method in the attached behavior is called. I then call dataform.Validate(). If validation passes I call dataform.Commit() which updates the dataobject in the form.
How do I then save the data to the database? I can't figure out how to go from the behavior to the view model to save the data.
The article I attached in my original post is from Syncfusion and it describes how to validate and commit the data but not how to get back to the view model to save the data.
The article itself shows the methods used to "save" and "cancel". You have to go to the github project file referenced in the article to see that the save and cancel functions are implemented in a behavior and not in the viewmodel.
I appreciate your help.
Carl
Hi Carl,
When the `CommitMode` is set to `Manual`, you need to explicitly call the `Commit` method to save the entered values into the `DataObject`. After calling `Commit`, you can retrieve these values by casting the `DataObject` to your model class. Here’s an example of how this can be done:
|
private async void OnSaveButtonClicked(object sender, EventArgs e) { if (this.dataForm != null && App.Current.MainPage != null) { if (this.dataForm.Validate()) { this.dataForm.Commit();
var dataObject = this.dataForm.DataObject as DataFormModel; string name = dataObject.Name; string email = dataObject.Email; DateTime date = dataObject.Date; string eventName = dataObject.EventName; string feedback = dataObject.FeedBack; } } }
|
We have included this code in the KB sample, displaying the values in an alert box, and have attached the updated sample for your reference.
Please review it and let us know if it meets your requirements. If you encounter any issues, kindly modify the sample to replicate the problem and share it with us for further investigation.
Regards,
Vidyalakshmi M.