Item Not Found
When trying to save items from my SfDataForm, items that are marked as [DataType(DataType.Date)] for example the item.Value is not present.
Here is my object decoration:
[Display(GroupName = "Basic", Name = "DOB")]
[DataFormDisplayOptions(RowOrder = 2, ItemsOrderInRow = 0)]
[Required(ErrorMessage = "DOB is required")]
[DataType(DataType.Date)]
public string DOB { get; set; }
And here is my On Save Method:
private async void saveButton_Clicked(object sender, EventArgs e)
{
Patient patient = new Patient();
if (this.patientDataForm.Items != null)
{
foreach (var viewItem in this.patientDataForm.Items)
{
if (viewItem is DataFormItem dataFormItem)
{
string fieldName = dataFormItem.FieldName;
string labelText = dataFormItem.LabelText;
var value = dataFormItem.GetValue();
}
if (viewItem is DataFormGroupItem groupItem)
{
var groupItems = groupItem.Items;
foreach (var item in groupItems)
{
string fieldName = item.FieldName;
string labelText = item.LabelText;
var value = item.GetValue();
if (fieldName == "DOB")
{
patient.DOB = (value != null) ? value.ToString() : " ";
Debug.WriteLine($"DOB Value: {patient.DOB}");
}
if (fieldName == "fname")
{
patient.fname = (value != null) ? value.ToString() : " ";
}
}
}
}
}
How do I find the object?
Hi Frederick,
Thank you for reaching out to us. To ensure the Date editor in SfDataForm correctly retrieves and binds the value, please define the `DOB` property as `DateTime` instead of a string. Here’s the correct approach:
|
[Display(GroupName = "Basic", Name = "DOB")] [DataFormDisplayOptions(RowOrder = 2, ItemsOrderInRow = 0)] [Required(ErrorMessage = "DOB is required")] [DataType(DataType.Date)] public DateTime DOB { get; set; }
private async void saveButton_Clicked(object sender, EventArgs e) { Patient patient = new Patient();
if (this.patientDataForm.Items != null) { foreach (var viewItem in this.patientDataForm.Items) { if (viewItem is DataFormItem dataFormItem) { string fieldName = dataFormItem.FieldName; string labelText = dataFormItem.LabelText; var value = dataFormItem.GetValue(); }
if (viewItem is DataFormGroupItem groupItem) { var groupItems = groupItem.Items;
foreach (var item in groupItems) { string fieldName = item.FieldName; string labelText = item.LabelText; var value = item.GetValue();
if (fieldName == "DOB") { if (value is DateTime dateValue) { Debug.WriteLine($"DOB Value: {dateValue.Date.ToString()}"); } }
if (fieldName == "fname") { patient.fname = (value != null) ? value.ToString() : " "; } } } } } }
|
For more information on DateEditor, please refer to our user guide documentation: Date-editor
We've attached a sample demonstrating this. Please review the attached sample and let us know if you need any further assistance.
Regards,
Vidyalakshmi M.
Attachment: MAUIDataForm_fa766e0c.zip
- 1 Reply
- 2 Participants
-
FS Frederick Switzer
- Mar 20, 2025 06:56 PM UTC
- Mar 21, 2025 10:45 AM UTC