This is a migrated thread and some comments may be shown as answers.

Nullable types in grid popup editor

1 Answer 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jesse
Top achievements
Rank 1
Jesse asked on 02 Nov 2012, 05:29 PM
Hello,

I have a grid that is bound to a model with nullable types (a bunch of "decimal?" types representing currency, to be exact). It is using a popup for editing. The editor template I am using for the popup hides some of these fields by default and only shows them if certain conditions are met based on other inputs.

At first, I would try to add an item to the grid and it didn't work--the popup wouldn't even close. Then I realized it was because these hidden currency fields were empty, and it was showing a validation message. The properties that are having this issue are not marked as [Required]. 

I've been trying for hours to find a solution to this problem that makes it so these fields are allowed to be null, or to specify some kind of default value.

If needed I can post a sample project. Any ideas?

1 Answer, 1 is accepted

Sort by
0
Ryan
Top achievements
Rank 1
answered on 21 Nov 2012, 10:53 PM
I came across a similar issue, but for me the validation seemed to be firing because my model property was not Nullable.  I changed my property from an int to a Nullable<int> and this seemed to prevent the required validation from firing, however it introduces another issue in my case; that being that that particular field was being set by a kendo dropdownlist, which doesnt seem to be handling setting the model/dataItem value properly when it is initially null (such as in an add).

When I had my grid in InLine/InCell edit mode, I was able to create a javascript workaround that would set the row's dataItem on change of the dropdownlist as follows:

function OnGridForeignKeyDropDownChange(e) {
    var grid = $(e.sender.element).parents(".k-grid").data("kendoGrid");
    var selectedRow = grid.select();
    var dataItem = grid.dataItem(selectedRow);
    var value = e.sender.value();
    dataItem.set(e.sender.element.attr("id"), value);
}
and then setting the Change event on my dropdownlist (note: this is done in my EditorTemplate for GridForeignKey, I am using MVC 4)
@(Html.Kendo().DropDownList()
    .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
    .OptionLabel(string.IsNullOrEmpty(ViewData.ModelMetadata.NullDisplayText) ? " " : ViewData.ModelMetadata.NullDisplayText)
    .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
    .Events(e => e.Change("OnGridForeignKeyDropDownChange")
                  .Open("FixBlankHeightInKendoDropDown")
           )
)
Unfortunately using a similar approach of calling OnGridForeignKeyDropDownChange on Change of the dropdownlist doesnt work (at least not generically like this) from a popup editor, as the dropdownlist is no longer inside the grid.

You could create a custom version of this function in your View that finds the grid by its name instead.  I am still attempting to determine if there is a way to find the grid that is the 'owner' of the edit popup generically so I dont have to replicate this code all over the place; perhaps some Kendo guru can provide some insight?

Final Note:  you might also notice the call to FixBlankHeightInKendoDropDown on open of the dropdownlist; this is an additional function that fixes the issue of when you have an empty OptionLabel for no selection in the dropdownlist.  For some reason the blank item in the dropdownlist gets rendered with no height when the text is empty.  Here is that function for your reference:
function FixBlankHeightInKendoDropDown() {
    var firstItem = $(this.ul[0].firstChild);
    if (jQuery.trim(firstItem.text()) == "") {
        firstItem.html(" ");
    }
}
Hope this helps....
Tags
Grid
Asked by
Jesse
Top achievements
Rank 1
Answers by
Ryan
Top achievements
Rank 1
Share this question
or