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

kendogrid return null to controller (IEnumerable<T> is null but when I use T there is only one element returned from view)

5 Answers 1256 Views
Grid
This is a migrated thread and some comments may be shown as answers.
hamid
Top achievements
Rank 1
hamid asked on 28 Dec 2014, 12:27 PM
Hi,

We are using KendoGrid in "inCell" mode with "Batch" mode enabled and according to telerik examples we've created our model like following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace webArta.Models
{
    public class ASoratModel
    {
 
        public long id { get; set; }
        public String name { get; set; }
        public String title { get; set; }
 
    }
}

Then I've create my BLL as following :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
 
namespace webArta.Models
{
    public class ASoratService
    {
        private String sqlCnStr = functions.GetConnectionString(false);
        public List<ASoratModel> Read()
        {
            List<ASoratModel> data = new List<ASoratModel>();
            using (SqlConnection connection = new SqlConnection(sqlCnStr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("select * from [test]", connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            ASoratModel m = new ASoratModel();
                            m.id = (long)reader["id"];
                            m.title = (string)reader["title"];
                            m.name = (string)reader["name"];
                            data.Add(m);
                        }
                    }
 
                }
            }
            return data;
        }
 
        public void Create(ASoratModel model)
        {
 
            model.name = "";
            model.title = "";
            String sql =
                "INSERT INTO [test] (name,title) VALUES ('" + model.name + "','" + model.title + "')";
            using (SqlConnection connection = new SqlConnection(sqlCnStr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
 
        }
 
 
 
        public void Update(ASoratModel model)
        {
            String sql =
               "update [test] set name='" +model.name+"',title='"+model.title+"' where id="+model.id;
            using (SqlConnection connection = new SqlConnection(sqlCnStr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
 
 
 
        public void Delete(ASoratModel model)
        {
 
        }
 
    }
}

and my controller as following :
  public ActionResult Read([DataSourceRequest] DataSourceRequest request)
        {
 
            return Json(aSoratService.Read().ToDataSourceResult(request));
        }
 
        [AcceptVerbs(HttpVerbs.Post)]
       
 
         [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update([DataSourceRequest] DataSourceRequest request,
            IEnumerable<ASoratModel> asorats
){
             foreach(var item in asorats)
             {
                 aSoratService.Update(item);
             }
     
 
    return Json(asorats.ToDataSourceResult(request,ModelState));
 
         }
        

and this is my view :
@using webArta.Models
@using System.Linq
@using System.Data
 
 @(Html.Kendo().Grid<webArta.Models.ASoratModel>()
              .Name("grid")
 
              .Columns(columns =>
              {
                  columns.Bound(p => p.name);
                  columns.Bound(p => p.title);
                  //columns.Command(c =>
                  //{
                  //    c.Edit();
                  //    c.Destroy();
                  //});
 
              })
              .ToolBar(t =>
              {
                  t.Save();
                  t.Create();
 
 
              })
 
                 
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .AutoSync(true)
                  .Batch(true)                  
 
                         .Model(m => m.Id(p => p.id))
                  .Read(r => r.Action("Read", "MY_CONTROLLER"))
                             .Create(update => update.Action("Create", "MY_CONTROLLER"))
 
                                    .Update(update => update.Action("Update", "MY_CONTROLLER"))
                    .Destroy(update => update.Action("EditingInline_Destroy", "MY_CONTROLLER"))
                  .ServerOperation(true)
 
 
              )
 
              .Navigatable()
               
                       .Pageable()
                      .Sortable()
                              .Editable(ed =>
                              {
 
                                  ed.Mode(GridEditMode.InCell);
                                  ed.CreateAt(GridInsertRowPosition.Bottom);
 
                              })
 
 
 
 
                      .Scrollable()
                      .Filterable()
                       
 
                    .Resizable(resize => resize.Columns(true))
      )

yet when we try to update the data inside the Grid the Update method will get null value for it's second argument (IEnumerable<ASoratModel> asorats) but when we omit the IEnumerable<T> we only get one item.

we want need to get collection of all items when they are updated for showing them correctly in the Grid.

We are confused since we can't find any clue where are we doing wrong.
One thing to note : we don't have any ViewModel class it is all we have that I've copied here.

5 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 29 Dec 2014, 03:29 PM
Hello Hamid,

The problem is caused by the fact that you have set AutoSync to true. In this case the Grid will make an update request for each modified cell and you will never get a list of data items in the Update action method. Instead, you will be getting just one data item, because only one data item is "dirty" at that time.

Normally, batch editing is NOT used with AutoSync set to true, because this defeats the purpose of batch editing.

Regards,
Dimo
Telerik

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

0
hamid
Top achievements
Rank 1
answered on 29 Dec 2014, 05:16 PM
Hi Dimo,

Yeah we fixed that issue but now there is another problem that we are facing now.
after doing update the focus will jump to [first row first cell] and it won't preserve it's own place.
I have checked the ModelState of above example and compared it with your kendo.grid.examples couldn't find any clue where is the problem.
both return sane JSON
0
Dimo
Telerik team
answered on 30 Dec 2014, 09:27 AM
Hello Hamid,

The focus is moved to the first row and cell by design when the Grid is rebound (i.e. its table is re-rendered). If you want to preserve the focus position, use the dataBinding and dataBound events to retrieve and restore the "current" cell. Note that you will need to save the row index and cell index of the old focused cell, instead of its DOM element reference, then retrieve the new cell, based on these indexes and set it as a "current" one.

http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#methods-current

Regards,
Dimo
Telerik

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

0
Soleh
Top achievements
Rank 1
answered on 13 Aug 2018, 11:45 AM

Man i am trying to use simple grid and showing a Date of birth in the grid,displaying fine but not sending the proper date to controller'action i am using asp.net core

 @(Html.Kendo().Grid<EmployeeDemo.Models.EmployeeViewModel>
            ()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.Name);
                columns.Bound(p => p.Dob).Format("{0:MM/dd/yyyy}");
                columns.Bound(p => p.Status).Width(150);
                columns.Command(command => command.Destroy()).Width(150);
            })
            .ToolBar(toolBar =>
            {
                toolBar.Create();
                toolBar.Save();
            })
            .Editable(editable => editable.Mode(GridEditMode.InCell))
            .Pageable()
            .Sortable()
            .Scrollable()
            .HtmlAttributes(new { style = "height:550px;" })
            .DataSource(dataSource => dataSource
            .Ajax()
            .AutoSync(false)
            .Batch(true)
            .PageSize(20)
            .ServerOperation(false)
            .Events(events => events.Error("errorHandler"))
            .Model(model =>
            {
                model.Id(p => p.Id);
                model.Field(p => p.Id).Editable(false);
                model.Field(p => p.Dob).Editable(true);
                model.Field(p => p.Name).DefaultValue(
        ViewData["defaultCategory"] as EmployeeDemo.Models.EmployeeViewModel);
            })
            .Read(read => read.Action("FillData", "Home"))
            .Update(update => update.Action("EditingCustom_Update", "Home"))
            .Create(create => create.Action("EditingCustom_Create", "Home"))
            .Destroy(destroy => destroy.Action("EditingCustom_Destroy", "Home"))
            )
        )

0
Georgi
Telerik team
answered on 15 Aug 2018, 08:08 AM
Hello Soleh,

A common cause for the described behavior is when cultures on server and client are different. Basically, the date sent from the client is not in the format the server expects it. For example when a date in dd/MM/yyyy format is sent on the client and the server uses culture which date format is MM/dd/yyyy, the server tries to parse the day as a month - 13/06/2018 is sent and the server will try to parse it to 6th of the 13th month - which is invalid.

To avoid the above behavior make sure that both server and client use the same culture. Please refer to the following article which demonstrates how to match both cultures:



Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
hamid
Top achievements
Rank 1
Answers by
Dimo
Telerik team
hamid
Top achievements
Rank 1
Soleh
Top achievements
Rank 1
Georgi
Telerik team
Share this question
or