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

MultiSelect "preselect" value, but also a default value

12 Answers 4075 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
iCognition
Top achievements
Rank 1
iCognition asked on 12 Nov 2015, 10:52 PM

I want to have a MultiSelect with a default value (the name of the current user), but I also do not want to bind the data as there are far too many users to load at once.

How can I have both? Currently, the default value overrides the pre-select value.

 

http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/multiselect/overview#pre-select-values-on-initial-loading​

12 Answers, 1 is accepted

Sort by
0
iCognition
Top achievements
Rank 1
answered on 13 Nov 2015, 02:50 AM
Another issue: with pre-selected value, I am able to select one of the values which has already been preselected, thus deleting both instances of that value.

For instance, in the example if you select Chai again by typing in Chai, it will be deleted.
0
Georgi Krustev
Telerik team
answered on 17 Nov 2015, 08:52 AM
Hello Matt,

Probably, I am missing something, but the MultiSelect widget can show selected items (pre-selected ones) without binding the widget. This is accomplished passing the whole objects as widget value: This is the same link, that you posted in your first message. Please note that the widget requires to be bound in order to operate properly. Could you elaborate a bit more on "default" value?

With regards to the second statement, there is a known issue related to the item selection. I suppose that it is related to the depicted issue:
Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
iCognition
Top achievements
Rank 1
answered on 19 Nov 2015, 11:40 PM
.Model(model =>
{
    model.Id(group => group.Id);
    model.Field(group => group.Administrators)
        .DefaultValue(new List<MemberViewModel>() { new MemberViewModel() { Id=CurrentUser.Id }});
})

 

My MultiSelect is in a grid, I set the DefaultValue of the MultiSelect with the Model parameter (see above)

This worked fine, until I had to set the value on the MultiSelect to prevent a huge initial lookup

@(Html.Kendo().MultiSelectFor(model => model)
    ..snip..
    .Value(Model.ToList()))

The value appears to be overwriting the DefaultValue, even when the value is blank

0
iCognition
Top achievements
Rank 1
answered on 19 Nov 2015, 11:58 PM

My last post was the wrong way around

DefaultValue overrides the Value of the MultiSelect

0
Georgi Krustev
Telerik team
answered on 23 Nov 2015, 12:17 PM
Hello Matt,

Indeed, the defaultValue of the model field will override the widget's value and this is expected. Basically, the model has default values and the widgets should honor those values. In other words, model default values have higher priority than the widgets values.

If you would like to have pre-defined values in the MultiSelect, then you will need to define a default selected objects in the model. Thus on create, the grid will create a new object using default values and will apply them to the widgets in the editor template.

Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
iCognition
Top achievements
Rank 1
answered on 23 Nov 2015, 09:48 PM
That should probably be documented, since it goes against the meaning of the word "default"...

Default: a preselected option adopted by a computer program or other mechanism when no alternative is specified by the user or programmer.
0
iCognition
Top achievements
Rank 1
answered on 23 Nov 2015, 10:14 PM
How can I set the model value before it has been created if I am using a server datasource? In my view model constructor I have initialized any lists that are being used, but I am unable to populate them with known default values since the constructor must be parameter less.

If I try to create the grid without the default value set I get this error: 

Value cannot be null. Parameter name: source
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ASP._Page_Views_Shared_EditorTemplates_GroupViewModel_cshtml.Execute() in GroupViewModel.cshtml:line 36 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()

Does this mean I can't use a server datasource?
0
iCognition
Top achievements
Rank 1
answered on 23 Nov 2015, 10:20 PM
GroupViewModel.cshtml:line 36 is @(Html.Kendo().Grid<GroupViewModel>()...
0
Georgi Krustev
Telerik team
answered on 26 Nov 2015, 09:13 AM
Hello Matt,

We definitely will document the "default value" behavior better in order to avoid further confusion in our users.

I am afraid that I cannot suggest any solution in this case, because I am not fully aware how the grid is actually configured and used in your application. Generally speaking, model default values are the ones used to setup the newly created model with predefined values. You need to use them to control the values of the empty (new) model. Please note that the Grid widget creates the new model on the client even when server datasource is used. That is why the schema.model configuration is important in all cases.

In oder to assist you further with this issue, please send us a repro demo.

Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
iCognition
Top achievements
Rank 1
answered on 27 Nov 2015, 01:47 AM
See attached. Had to remove the kendo lib to be under the file size limit...
0
iCognition
Top achievements
Rank 1
answered on 27 Nov 2015, 01:48 AM
See attached, kendo lib removed to be under the file size limit
0
Georgi Krustev
Telerik team
answered on 30 Nov 2015, 12:10 PM
Hello Matt,

Thank you for the repro demo. Indeed, the widget will lose its pre-selected value when it has an initial value (options.value) and model field has a default value too. I logged the case for further investigation and fix. We will try to address the limitation in some of our next official releases.

For now, the best solution is to leave the grid's model to take control over the UI editors:
@(Html.Kendo().MultiSelectFor(m => m.Members)
        .DataValueField("Id")
        .DataTextField("Name")
        .Placeholder("Type user's name...")
        .Filter(FilterType.Contains)
        .AutoBind(false)
        .MinLength(3)
        .DataSource(source =>
        {
            source.ServerFiltering();
            source.Read(read =>
            {
                read.Action("ReadMembers", "Home");
            });
        })
        .Value(new string[] {}))
    @Html.ValidationMessageFor(model => model.Members)

Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
MultiSelect
Asked by
iCognition
Top achievements
Rank 1
Answers by
iCognition
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or