Grid save does not allow blank field

1 Answer 43 Views
Grid
Bruce
Top achievements
Rank 1
Bruce asked on 07 Sep 2023, 03:37 PM

When updating a record in the grid - it does not allow me to save an empty string for that field - if the previous value in the field contained a value.

My underlying class property doesn't work with either of the following options

with annotations:     

[Display(Name = "Comments")]
[Required(AllowEmptyStrings = true)]
public string ItemComments { get; set; } = " ";

Without annotations:

public string ItemComments { get; set; } = " ";

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 12 Sep 2023, 12:36 PM

Hello Bruce,

When editing an existing Grid record and leaving the "ItemComments" field as an empty string, it is sent from the client as an empty string, as per the screenshot below:

 

However, the server-side has the null value assigned:


This is due to the fact that string is a reference type, and its default value is null. The ModelBinder sets the properties to their default value if no value is provided in the request.

You could handle this by decorating the field in the C# model with the following data annotation:

[Display(Name = "Comments")]
[Required(AllowEmptyStrings = true)]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string ItemComments { get; set; } = " ";

As a result, when an empty string value is submitted, the modified record will be returned back to the Grid with the same value

I hope you find this helpful.

Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Bruce
Top achievements
Rank 1
commented on 12 Sep 2023, 02:38 PM | edited

Thanks Mihaela,

Unfortunately this didn't solve the issue because the grid is prompting me for non-blank fields when I press the update button so it is not even getting to the post event / server side but stops already on the client-side.

Mihaela
Telerik team
commented on 15 Sep 2023, 11:12 AM

Thank you for the update, Bruce.

To replicate the described issue at my end, I created a demo project that contains an In-Line editable Grid. The "ItemComments" property is defined as non-required:

[Display(Name = "Comments")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string ItemComments { get; set; } = " ";

It appears that the required validation does not trigger when editing an existing Grid record at my end.

Would you please modify the attached runnable sample based on your setup to reproduce the behavior you are experiencing and send it back in the thread? I will test it locally and follow up with the respective solution.

Thank you for your cooperation.

Best,

Mihaela

Tags
Grid
Asked by
Bruce
Top achievements
Rank 1
Answers by
Mihaela
Telerik team
Share this question
or