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

Kendo Grid not work to read data

1 Answer 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nattapong
Top achievements
Rank 1
Nattapong asked on 25 Jun 2013, 03:43 AM
Hi, I'm try to use Kendo grid follow demos This --> http://demos.kendoui.com/web/grid/editing-popup.html

But Gird cann't Bind data 

Follow Controllers, and cshtml


public ActionResult Section()
       
{
           return View();
       }
 
       public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
       {
           return Json(db.Sections.Where( s=>s.IsPublished == true).ToList().ToDataSourceResult(request));
       }
 
       [AcceptVerbs(HttpVerbs.Post)]
       public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, Section section)
       {
           if (section != null && ModelState.IsValid)
           {
               db.Sections.Add(section);
               db.SaveChanges();
           }
 
           return Json(new[] { section }.ToDataSourceResult(request, ModelState));
       }
 
       [AcceptVerbs(HttpVerbs.Post)]
       public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, Section section)
       {
           if (section != null && ModelState.IsValid)
           {
               var target = db.Sections.FirstOrDefault( s => s.SectionID == section.SectionID);
               if (target != null)
               {
                   target.Name = section.Name;
                   target.Description = section.Description;
 
                   db.Entry<Section>(section).State = System.Data.EntityState.Modified;
                   db.SaveChanges();
               }
           }
 
           return Json(ModelState.ToDataSourceResult());
       }
 
       [AcceptVerbs(HttpVerbs.Post)]
       public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, Section section)
       {
           if (section != null && ModelState.IsValid)
           {
               var target = db.Sections.FirstOrDefault(s => s.SectionID == section.SectionID);
               if (target != null)
               {
                   target.IsPublished = false;                 
 
                   db.Entry<Section>(section).State = System.Data.EntityState.Modified;
                   db.SaveChanges();
               }
           }
           return Json(ModelState.ToDataSourceResult());
       }
@(Html.Kendo().Grid<MVC4Kendo.Models.Section>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Name);
        columns.Bound(p => p.Description).Width(100);
        columns.Bound(p => p.IsPublished).Width(100);      
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .Pageable()
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.SectionID))
        .Create(update => update.Action("EditingPopup_Create", "Grid"))
        .Read(read => read.Action("EditingPopup_Read", "Grid"))
        .Update(update => update.Action("EditingPopup_Update", "Grid"))
        .Destroy(update => update.Action("EditingPopup_Destroy", "Grid"))
    )
)
<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>


1 Answer, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 26 Jun 2013, 10:49 AM
Hi Nattapong,

From the provided information it's not clear for us what is the exact reason for this behavior as the current setup looks valid. Could you please provide runable project where the issue is reproduced? This would help us pinpoint the exact reason for this behavior.

Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Nattapong
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or