Gyuzal
Gyuzal

Reputation: 1591

Kendo Multiselect with composite dataTextField

I'd appreciate if someone could advise on the following:

My Multiselect:

 @Html.Kendo().MultiSelectFor(model => model.PAYMENT_METHOD).BindTo(paymentMethods).DataTextField("TITLE").DataValueField("CODE")

The dataSource looks like this:

CODE     TITLE
  1       abc
  2       def

Is it possible to have composite DataTextField , specifically like: 1 - abc, 2 - def, etc., i.e "CODE" - "TITLE"?

I know I could create a select list and define the format of textfield, but maybe there is another way? Thanks!

Upvotes: 1

Views: 1472

Answers (2)

Jeff
Jeff

Reputation: 304

You could also just add a search param to your datasource class that doesn't need to be mapped to your database. But evaluates the instance of the object and creates this composite field you can use to search. I have done this for times when searches are needed on FirstName and LastName.

public class YourObjectModel 
{
    public string Code { get; set; }
    public string Title { get; set; }
    [NotMapped]
    public string SearchTerm => $"{Code}{Title}"
}

Then set .dataTextField("SearchTerm") and there you go.

Upvotes: 0

Andrei V
Andrei V

Reputation: 7508

You could specify a template for the display item (you would probably want to take the text field out then):

 @Html.Kendo().MultiSelectFor(model => model.PAYMENT_METHOD)
              .BindTo(paymentMethods)
              //.DataTextField("TITLE")
              .DataValueField("CODE")
              .ItemTemplate("#= CODE# #=' - '# #= TITLE#")

Here's the link to the ItemTemplate method and the link to the general template methods.

Upvotes: 3

Related Questions