MultiSelectFor with MVC EF many-to-many relation

6 posts, 0 answers
  1. Erik
    Erik avatar
    24 posts
    Member since:
    Sep 2013

    Posted 09 Oct 2013 Link to this post

    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?
  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 11 Oct 2013 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. Erik
    Erik avatar
    24 posts
    Member since:
    Sep 2013

    Posted 11 Oct 2013 Link to this post

    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)
  5. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 15 Oct 2013 Link to this post

    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!
  6. Mike
    Mike avatar
    1 posts
    Member since:
    Dec 2011

    Posted 05 Nov 2014 in reply to Daniel Link to this post

    Can I get a copy of that sample project? 
  7. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 07 Nov 2014 Link to this post

    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!
     
Back to Top
Kendo UI is VS 2017 Ready