Entity Framework to Datasource

4 posts, 0 answers
  1. Roland
    Roland avatar
    2 posts
    Member since:
    Aug 2012

    Posted 14 Aug 2012 Link to this post

    Hallo,
    it's me first step in Kendo Web and I have a big Problem. (Sorry for my bad englisch)
    I have a APS.NET MVC 4 Projekt. 

    In the @Code -Area I get the data with Linq. I convert it also to a Json-String

    @Imports Newtonsoft.Json
    @code

      ' -- die Teams einlesen
      Dim ccT = (From p2 In ctx.TeamSet Order By p2.TeamName Select p2.TeamName, p2.Id).ToList()
      Dim ccTeams = JsonConvert.SerializeObject(ccT , Formatting.None)
    End Code

    Then I have the HTML-Code for my KendoDropDownList

    <input id="ecolor" value="1" />

    And now my Script

    <script>
        $(document).ready(function () {
     
            // create DropDownList from input HTML element
            $("#ecolor").kendoDropDownList({
                dataTextField: "TeamName",
                dataValueField: "Id",
                dataSource: '@ccTeams',
                index: 0,
                change: onChange2
            });
     
            var color2 = $("#ecolor").data("kendoDropDownList");
            function onChange2() {
                var value = $("#ecolor").val();
                alert(value)
            }
        });
     
    </script>

    That dosn't work. The DropDownBox is empty. What is wrong? 
    Thanks in advance 

    Roland




  2. John DeVight
    John DeVight avatar
    209 posts
    Member since:
    Jan 2010

    Posted 14 Aug 2012 Link to this post

    Hi Roland,

    The datasource that was getting passed in was a string and it needs to be a JSON array object.  To get it to work, I parse the ccTeams using JSON.parse.  Here is the code:
    $("#ecolor").kendoDropDownList({
        dataTextField: "TeamName",
        dateValueField: "Id",
        dataSource: JSON.parse('@Html.Raw(ccTeams)'),
        index: 0,
        change: onChange2
    });

    Note that I also had to use the Html.Raw to prevent HTML encoding by the Razor view engine.

    Attached is a sample application... using MVC 3.

    Hope that helps.

    Regards,

    John DeVight
  3. Kendo UI is VS 2017 Ready
  4. Roland
    Roland avatar
    2 posts
    Member since:
    Aug 2012

    Posted 14 Aug 2012 Link to this post

    Oh yeh.
    So simple. 
    It works. Many thanks

    Roland
  5. John DeVight
    John DeVight avatar
    209 posts
    Member since:
    Jan 2010

    Posted 14 Aug 2012 Link to this post

    Your welcome :-)

    When you have a moment, could you mark my response as the answer?  Much appreciated.

    Regards,

    John DeVight
Back to Top
Kendo UI is VS 2017 Ready