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

Entity Framework to Datasource

3 Answers 186 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Roland
Top achievements
Rank 1
Roland asked on 14 Aug 2012, 06:46 AM
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




3 Answers, 1 is accepted

Sort by
0
John DeVight
Top achievements
Rank 1
answered on 14 Aug 2012, 11:56 AM
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
0
Roland
Top achievements
Rank 1
answered on 14 Aug 2012, 01:53 PM
Oh yeh.
So simple. 
It works. Many thanks

Roland
0
John DeVight
Top achievements
Rank 1
answered on 14 Aug 2012, 02:13 PM
Your welcome :-)

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

Regards,

John DeVight
Tags
General Discussions
Asked by
Roland
Top achievements
Rank 1
Answers by
John DeVight
Top achievements
Rank 1
Roland
Top achievements
Rank 1
Share this question
or