Gyuzal
Gyuzal

Reputation: 1591

Kendo UI: Get MultiSelect selected values as a comma separated string

I'd appreciate if someone could help with my issue. I have an entity with field PAYMENT_CURRENCIES of string type, that should store comma separated values, i.e. "USD,EUR,AED" (or any other separation char). In my View:

 @Html.Kendo().MultiSelectFor(model => model.Contract.PAYMENT_CURRENCIES).BindTo(context.Currencies).DataTextField("CODE").DataValueField("CODE").Placeholder("Add currency...")

The problem is when I submit the form i receive only first selected value in the Controller. I would not like to change the datatype of the field for IEnumerable.

Is there a way to receive all selected values as a string with some separator?

Thanks a lot

Upvotes: 1

Views: 3451

Answers (3)

Kai Hartmann
Kai Hartmann

Reputation: 3154

Add an array-property to your model:

public string[] PAYMENT_CURRENCIES_LIST
{
    get
    {
        return PAYMENT_CURRENCIES?.Split(',');
    }
    set
    {
        PAYMENT_CURRENCIES = string.Join(",", value);
    }
}

Then use this property in your view:

@Html.Kendo().MultiSelectFor(model => model.Contract.PAYMENT_CURRENCIES_LIST)...

So the array-property maps to the Kendo-Multiselect and translates the values to/from the original field.

Upvotes: 1

RayanZ
RayanZ

Reputation: 13

I had the same requirement as yours, & couldn't find a decent solution, That's how I solved it:

  • Create a new Property in your ViewModel public List<string> SelectedCurrencies { get; set; }
  • Configure your MultiSelect Kendo helper to bind to the newly created property @Html.Kendo().MultiSelectFor(model => model.SelectedCurrencies) .BindTo(context.Currencies) .DataTextField("CODE") .DataValueField("CODE") .Placeholder("Add currency...")
  • To Save: Now when you hit your action method, just set your comma separated field PAYMENT_CURRENCIES = string.Join(",", viewModel.SelectedCurrrencies);
  • To Read: SelectedCurrencies = PAYMENT_CURRENCIES.Split(',').ToList();

Upvotes: 0

Sebastien Kovacs
Sebastien Kovacs

Reputation: 213

I don't think that you can automatically convert your multi select input value(s) to a single string.

So what you can do is:

  • Use a viewModel (ContractViewModel) which contains a List
  • Or use javascript to "convert" your input value(s) to a single string separated with any separator you want

Upvotes: 1

Related Questions