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

How to bind datasource to MVC Model

1 Answer 789 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Russell
Top achievements
Rank 1
Russell asked on 01 Jul 2015, 06:11 PM

I have Kendo UI Professional and I am trying to create a KendoGrid using data from the ViewData.Model
My script looks like this:

@model IEnumerable<WebApplication5.Models.Presentation>
..
<script>

 $(document).ready(function () {

            var dataSource = new kendo.data.DataSource({
                pageSize: 21,
                data: @Model,
                dataType: JSON,
                schema: {
                    model: {
                         fields: {
                                    Name: {},
                                    PresentationFamilyId: {},
                                    SourcePresentationId: {}
                        }
                    }
                }
            });

            $("#grid").kendoGrid({
                dataSource: dataSource,
                height: 550,
                groupable: true,
                sortable: true,
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 5
                },
                columns: [{
                    field:"Name",
                    title:"Name"
                }, {
                    field:"PresentationFamilyID",
                    title:"Family"
                }, {

                    field:"SourcePresentationID",
                    title:"Source"
                }]
             });
        });

</script>

 My
controller looks like this:

    public ActionResult Index()
    {
         return View(db.Presentations);
    }

I cannot get it to work. I have tried every permutation for DataSource.data that I can think of
but they all generate errors. For instance, the above code complains about “Unexpected token ]”

Tried in controller:            return View(db.Presentations.ToArray()); -- Unexpected Token ]
Changed javascript:

                                        data: @Model.ToArray()     -- Unexpected Token ]

                                        data:  @Model.ToList()

                                                   – The browser shows: data: System.Collections.Generic.List`1[WebApplication5.Models.Presentation],
                                                      with error:
                                                               Unterminated template literal 

                                                      (this is because of the unmatched quote mark)
How do I
specify @Model as my datasource?

1 Answer, 1 is accepted

Sort by
1
Russell
Top achievements
Rank 1
answered on 01 Jul 2015, 07:00 PM

Okay, I found my own answer: I have to convert to JSON

data:@Html.Raw(Json.Encode(@Model.Take(20000))),

Since my data set was large, I had to limit the rows returned (I will be smarter in final version of code)

I also, increased increased maxJsonLength in web.config as so:

<configuration>

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

 

Tags
General Discussions
Asked by
Russell
Top achievements
Rank 1
Answers by
Russell
Top achievements
Rank 1
Share this question
or