Mikhail Kostiuchenko
Mikhail Kostiuchenko

Reputation: 10541

How to send selected values to viewModel from multiselect (Kendo MultiSelect)

I'm using Kendo MultiSelect in my mvc5 project. So I have a View with multiselect:

@model Library.ViewModels.Models.BookViewModel

@{
    ViewBag.Title = "Edit";
}
<script>
    $(document).ready(function () {
        $("#multiselect").kendoMultiSelect({
            placeholder: "--Select Public Houses--",
            dataTextField: "PublicHouseName",
            dataValueField: "PublicHouseId",
            autoBind: true,
            dataSource: {
                transport: {
                    read: {
                        dataType: "json",
                        url: "/book/getallpublichouses"
                    }
                }
            }
        });
    });
</script>

And I have 2 viewModels:

public class BookViewModel
    {
        public int BookId { get; set; }

        public string Name { get; set; }

        public string AuthorName { get; set; }

        public int YearOfPublishing { get; set; }

        public  ICollection<PublicHouseViewModel> PublicHouses { get; set; }

    }



public class PublicHouseViewModel
    {
        public int PublicHouseId { get; set; }

        public string PublicHouseName { get; set; }

        public string Country { get; set; }

        public  ICollection<BookViewModel> Books { get; set; }
    }

In my Kendo MultiSelect a get all Public Houses from Book controller in JSON format. Next I selected some values:

So, how can I pass this selected values in public ICollection<PublicHouseViewModel> PublicHouses { get; set; } property in BookViewModel ?

enter image description here

Upvotes: 2

Views: 1666

Answers (1)

Dzanan Begovic
Dzanan Begovic

Reputation: 160

You can use:

public int[] PublicHouses { get; set; }

instead of:

 public  ICollection<PublicHouseViewModel> PublicHouses { get; set; }

Or you can create a new field in BookViewModel only for posting. When you are posting values from kendoMultiSelect, he posts only "dataValueField". After you post only id's you can do the rest of the logic in POST action.

It depends how you implemented your POST action and also on the relationship between two tables: Is it 1...N, or N....N.

Upvotes: 1

Related Questions