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

Inconsistent behaviour?

2 Answers 52 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Daniel Blendea
Top achievements
Rank 1
Daniel Blendea asked on 16 Dec 2019, 11:59 AM

I have a model with SourceId, LanguageId, OriginalLanguageId, which are bound to some dropdownlists, using :

01.@(Html.Kendo().DropDownListFor(m => m.SourceId)
02.    .DataTextField("Name")
03.    .DataValueField("Id")
04.    .ValuePrimitive(true)       <------ Here be dragons?
05.    .Animation(false).AutoBind(false)
06.    .HtmlAttributes(new
07.    {
08.        @class = "form-control",
09.        data_bind = "value: regulation.SourceId"
10.    })
11.    .OptionLabel("Please select a source")
12.    .Filter(FilterType.Contains)
13.    .DataSource("sourcesDataSource")
14.)
15. 
16.@(Html.Kendo().DropDownListFor(m => m.LanguageId)
17.    .DataTextField("Name")
18.    .DataValueField("Id")
19.    .Animation(false).AutoBind(false)
20.    .HtmlAttributes(new
21.    {
22.        @class = "form-control",
23.        data_bind="value: regulation.LanguageId"
24.    })
25.    .OptionLabel("Please select the official language")
26.    .Filter(FilterType.Contains)
27.    .DataSource("languagesDataSource")
28.)
01.@(Html.Kendo().DropDownListFor(m => m.OriginalLanguageId)
02.    .DataTextField("Name")
03.    .DataValueField("Id")
04.    .Animation(false).AutoBind(false)
05.    .HtmlAttributes(new
06.    {
07.        @class = "form-control",
08.        data_bind="value: regulation.OriginalLanguageId"
09.    })
10.    .OptionLabel("Please select the original language")
11.    .Filter(FilterType.Contains)
12.    .DataSource("languagesDataSource")
13.)

 

All 3 fields are int, non nullable, yet only LanguageId and OriginalLanguageId are bound and displayed/selected in their dropdowns.

The data structures for Language and Source have only {Id: int, Name: string}.

SourceId is bound, but is not displayed/selected, unless I set ValuePrimitive(true).

I see that it is bound because when I click the dropdown, the value is selected in the list.

So I would like to understand why do I have to set ValuePrimitive(true) only for a field.

 

Thank you.

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Daniel Blendea
Top achievements
Rank 1
answered on 16 Dec 2019, 12:41 PM

Some more information:

the "sourcesDataSource" is declared like this:

01.let sourcesDataSource = new kendo.data.DataSource({
02.    serverFiltering: false,
03.    transport: {
04.            read: {
05.            url: "@ViewData["ApiUrl"]/sources/sources-for-dropdowns",
06.            dataType: "json",
07.            type: "GET",
08.            data: function() {
09.                return { datasetId : viewModel.regulation.DatasetId }
10.            }
11.        },
12.    }
13.})

 

languagesDataSource is declared via ASP .NET Core, because it doesn't need any input parameter:

01.@(Html.Kendo().DataSource<LanguageDtoForFiltering>()
02.    .Name("languagesDataSource")
03.    .Custom(tr =>
04.    {
05.        tr.Transport(transport =>
06.        {
07.            transport.Read(read =>
08.            {
09.                read.Url(ViewData["ApiUrl"] + "/languages/languages-for-dropdowns");
10.            })
11.            .Cache(true);
12.        }).ServerFiltering(false);
13.    })
14.)

 

And the viewModel is bound like this:

01.$(function () {
02. 
03.    languagesDataSource.read();
04. 
05.    viewModel.regulationDataSource.fetch(function() {
06. 
07.        viewModel.set("regulation", this.data()[0]);
08.        kendo.bind($("#edit-regulation"), viewModel);
09. 
10.        // sourcesDataSource.read();
11.    });
12.});

 

 

 

 

 

 

 

 

0
Veselin Tsvetanov
Telerik team
answered on 19 Dec 2019, 11:35 AM

Hi Daniel,

The reason for the difference observed is the fact that you have called the read() on the Languages DataSource. At the same time, the remote for the Sources DataSource is not retrieved explicitly. Hence, it will not populate initially the selected value. In order to avoid that, you should also call read() on the sourcesDataSource:

$(function () {
	languagesDataSource.read();
	sourcesDataSource.read();
});

Attached you will find a small sample implementing the above suggestion.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Tags
DropDownList
Asked by
Daniel Blendea
Top achievements
Rank 1
Answers by
Daniel Blendea
Top achievements
Rank 1
Veselin Tsvetanov
Telerik team
Share this question
or