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

Basic Grid Functions Not Working

1 Answer 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jordan
Top achievements
Rank 1
Jordan asked on 16 Jul 2014, 07:44 PM
So I set up a simple Kendo Grid, which I want to use to batch edit in cells etc. However when I click Add New Record the site redirects me to http://localhost:52536/Tag/Tag_Read/10322?grid-mode=insert with a page displaying a text list (json?) of my db entries. The in cell editing also doesn't work at all. 
I'll post my view and controller below. 


View:

@model IEnumerable<HoldAndRelease.Models.Tag>


<br />
<br />
<br />
@(Html.Kendo().Grid(Model) //Bind the grid to ViewBag.Products
      .Name("grid")
      .Columns(columns =>
      {
          // Create a column bound to the ProductID property
          columns.Bound(tag => tag.TagID);
          columns.Bound(tag => tag.ProductionDate);
          columns.Bound(tag => tag.CodeDate);
          columns.Bound(tag => tag.Amount);
          columns.Command(cmd => cmd.Edit());

      })
      .ToolBar(toolbar =>
      {
          toolbar.Create();
          toolbar.Save();
      })
      .Pageable()
      .Navigatable()
      .Sortable()
      .Scrollable()
      .DataSource(datasource =>
          datasource
          .Ajax()
          .Batch(true)
          .PageSize(10)
          .ServerOperation(false)
          .Events(events => events.Error("error_handler"))
          .Model(model =>
          {
              model.Id(tag => tag.TagID);
              model.Field(tag => tag.TagID).Editable(false);
          })
          .Create(create => create.Action("Tag_Create", "Tag"))
          .Read(read => read.Action("Tag_Read", "Tag"))
          .Update(update => update.Action("Tag_Update", "Tag"))
          ).Editable(editable => editable.Mode(GridEditMode.InCell))
)

<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>

Controller:

public ActionResult Tag_Read([DataSourceRequest]DataSourceRequest request)
        {
            db.Configuration.ProxyCreationEnabled = false;
            IQueryable<Tag> tags = db.Tags;
            DataSourceResult result = tags.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet); 
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Tag_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
        {
            db.Configuration.ProxyCreationEnabled = false;
            var entities = new List<Tag>();
            if (ModelState.IsValid)
            {
                foreach (var tag in tags)
                {
                  
                    var entity = new Tag
                    {
                        ProductionDate = tag.ProductionDate,
                        CodeDate = tag.CodeDate,
                        Amount = tag.Amount
                    };

                    db.Tags.Add(entity);

                    entities.Add(entity);
                }

                db.SaveChanges();
            }

            return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag 
            {
                TagID = tag.TagID,
                ProductionDate = tag.ProductionDate, 
                CodeDate = tag.CodeDate,
                Amount = tag.Amount
            }), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Tag_Update([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
        {
            db.Configuration.ProxyCreationEnabled = false;
            var entities = new List<Tag>();
            if (ModelState.IsValid)
            {
                foreach (var tag in tags)
                {
                    var entity = new Tag
                    {
                        ProductionDate = tag.ProductionDate,
                        CodeDate = tag.CodeDate,
                        Amount = tag.Amount
                    };

                    entities.Add(entity);

                    db.Tags.Attach(entity);

                    db.Entry(entity).State = EntityState.Modified;
                }
                
                db.SaveChanges();
            }

            return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag
            {
                TagID = tag.TagID,
                ProductionDate = tag.ProductionDate,
                CodeDate = tag.CodeDate,
                Amount = tag.Amount
            }), JsonRequestBehavior.AllowGet);
        }

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 18 Jul 2014, 12:42 PM
Hi,

The code looks correct. The described problems indicate that the widget is not initialized which could be caused by a JavaScript error. Could you check if there are any errors?

Regards,
Daniel
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.

 
Tags
Grid
Asked by
Jordan
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or