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

MultiSelectFor with MVC EF many-to-many relation

5 Answers 296 Views
MultiSelect
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 09 Oct 2013, 10:54 PM
I have an MVC 4, EF 5 model first project with these models:
  • Contractor
  • Profile
Between these two is a many-to-many relation. (in the background EF generates a ContractorProfiles table and I'm can get a list of profiles for a contractor by calling Contractor.Profiles)
I would like to use the MultiSelectFor to be able to select one or more profiles for my contractor and directly bind it to my viewmodel but I don't know how to do this.
How can this be done?

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 11 Oct 2013, 02:09 PM
Hello Erik,

The MultiSelectFor helper should automatically bind the values based on model collection of objects in the latest release. Which version are you using? If you are using the latest release then could you provide the code that you are currently using and more detailed information about the problem.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Erik
Top achievements
Rank 1
answered on 11 Oct 2013, 03:24 PM
Hi Daniel,

Yes, I'm using the latest version. Like I said, I don't know how to do this so I do not have an example.
In the end I just used the MultiSelect and some extra code on the server to get it working. 
@(Html.Kendo().MultiSelect()
      .Name("Contractor.Profile")
      .BindTo(Model.ProfileList)
      .DataValueField("Id")
      .Value(Model.CurrentProfiles)
)
But I would like to see an example of the MultiSelectFor.
So the data from my list is in Model.Profiles and the result from the list should be bound to Contractor.Profiles (this is a collection generated by EF)
0
Daniel
Telerik team
answered on 15 Oct 2013, 10:59 AM
Hello,

You should use a ViewModel for the MultiSelect data. Otherwise, the JavaScriptSerializer will thrown an exception for circular references. I attached a sample project using the MultiSelectFor helper and Entity Framework for the Northwind database.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mike
Top achievements
Rank 2
answered on 05 Nov 2014, 10:41 AM
Can I get a copy of that sample project? 
0
Daniel
Telerik team
answered on 07 Nov 2014, 10:25 AM
Hello Mike,

The project is attached but for some reason it seems that it is not publicly visible. I have reattached it to this post.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MultiSelect
Asked by
Erik
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Erik
Top achievements
Rank 1
Mike
Top achievements
Rank 2
Share this question
or