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

Create action and event Issue

4 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gaetano
Top achievements
Rank 1
Gaetano asked on 21 Feb 2014, 09:37 AM
Hi Guys, I found a wierd behavior on my grid:

@(Html.Kendo().Grid<MyEntity>(Model)
       .Name("valueGrid")
       .ToolBar(commands => commands.Create().Text("Add new value"))
       .Columns(columns =>
       {
           columns.Bound(c => c.DOMAINID).Visible(false);
           columns.Bound(c => c.CODE);
           columns.Bound(c => c.VALUE);
           columns.Command(command => { command.Edit().UpdateText("Save"); command.Destroy().Text("Delete"); });
       })
       //.Events(e => e.SaveChanges("OnSaveChanges"))
       .Sortable()
       .Scrollable()
       .DataSource(dataSource => dataSource       
       .Ajax()
       .ServerOperation(false)      
       .Model(m => m.Id(v => v.DOMAINID))
       .Update(update => update.Action("UpdateValue", "DomainValue"))
       .Create(create => create.Action("CreateValue", "DomainValue"))
       .Destroy(delete => delete.Action("DeleteValue", "DomainValue"))
    )
   )

If I enable my Event, once I click on add Button, the whole page is raplaced by the result of the action/controller in the href of the anchor
say "/DomainValue/Details/2?valueGrid-mode=insert" (that would be the grid itself)

While if I disable the event, I get my inline add with no issue....

Am I doing somthing wrong??
I need the event because I somehow need to have the hidden value (DOMAINID) set to the same of the other lines. The problem is that my grid change based on another control; pratically this grid displays the values of some domain tables; so the Id Have to be fixed and it cannot be fixed as a default value in the model scheme.

thanks
Fabio

4 Answers, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 25 Feb 2014, 08:00 AM
Hello Fabio,

This behavior is indeed abnormal, however the provided code snippet does not show anything that might cause it. Are there any errors in the browser's console? Also, providing a runnable project where this happens would be the best case scenario, as we would be able to reproduce this behavior locally, however sharing the entire View and the Controller would help as well. 

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gaetano
Top achievements
Rank 1
answered on 25 Feb 2014, 09:53 AM
Hi Alexander, I'll post the whole grid and controller; if that will not prove to be usefull i'll write a local example.

@model IEnumerable<Entity>
 
@using Kendo.Mvc.UI
 
<div class="detailValuesForm">
 
       @(Html.Kendo().Grid<Entity>(Model)
        .Name("valueGrid")
        .ToolBar(commands => commands.Create().Text("Add new value"))
        .Columns(columns =>
        {
            columns.Bound(c => c.DOMAINID).Visible(false);
            columns.Bound(c => c.CODE);
            columns.Bound(c => c.VALUE);
            columns.Command(command => { command.Edit().UpdateText("Save"); command.Destroy().Text("Delete"); });
        })
        //.Events(e => e.SaveChanges("OnSaveChanges"))
        .Sortable()
        .Scrollable()
        .DataSource(dataSource => dataSource       
        .Ajax()
        //.Events(e => e.Error("OnDatasourceError"))
        .ServerOperation(false)      
        .Model(m => m.Id(v => v.CODE))
        .Update(update => update.Action("UpdateValue", "DomainValue"))
        .Create(create => create.Action("CreateValue", "DomainValue"))
        .Destroy(delete => delete.Action("DeleteValue", "DomainValue"))
     )
    )
 
</div>
 
<script>
    function OnSaveChanges(e) {
        alert(e);
    }
 
    function OnDatasourceError(e) {
        alert(e);
    }
</script>


this view is loaded inside the change event of a listview like this:

$.ajax({
        url:  "../DomainValue/Details/" + id,
        type: 'GET',
        datatype: 'html',
    }).done(function (success) {
        $("section.DomValues").find("div.detailValuesForm").remove();
        $("section.DomValues").append(success);
    }).fail(function (jqXHR, textStatus) {
        if (jqXHR.status = "404") {
            $("section.DomValues").find("div.detailValuesForm").remove();
            $("section.DomValues").append($("<div>").addClass("detailValuesForm").text("No value associated."))
        }
    });

Here is the controller:
The first action returns the view, the others are the ones associated to grid's transport actions.

public ActionResult Details(int id = 0)
{
    return View(db.Entity.Where(v => v.DOMAINID == id));
}
 
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateValue([DataSourceRequest] DataSourceRequest request, Entity domValue)
{
    if (ModelState.IsValid)
    {
        db.Entity.Add(domValue);
        db.SaveChanges();
    }
    return Json(new[] { domValue }.ToDataSourceResult(request, ModelState));
}
 
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DeleteValue([DataSourceRequest] DataSourceRequest request, Entity domValue)
{
    if (ModelState.IsValid)
    {
        db.Entry(domValue).State = EntityState.Deleted;
        db.SaveChanges();
    }
    return Json(new[] { domValue }.ToDataSourceResult(request, ModelState));
}
 
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateValue([DataSourceRequest] DataSourceRequest request, Entity domValue)
{
    if (domValue != null && ModelState.IsValid)
    {
        db.Entry(domValue).State = EntityState.Modified;
        db.SaveChanges();
    }
 
    return Json(new[] { domValue }.ToDataSourceResult(request, ModelState));
}

Thanks
Fabio
0
Alexander Popov
Telerik team
answered on 27 Feb 2014, 08:57 AM
Hello again Fabio,

Thank you for code snippets. Replacing the entire page could happen in case Server editing is used, however that is not the case here. I tried reproducing this using similar configuration and I always got the expected results, regardless of whether there are any Save or Change event handlers attached to the Grid. I would have to ask you for a runnable project so I can further investigate what is causing this.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gaetano
Top achievements
Rank 1
answered on 28 Feb 2014, 11:14 AM
Hi Alexander,
Unfortunately, to reproduce this issue I'd spend too much time. And I'm not so sure to be able to reproduce it anyway (not using real entity from ours db).

By the way, I managed to resolve this by myself, I just tried to put grid's events handler (and and immediate function for custom validation) out of the view and inside a js bundle (load by the same view)...
That Fixed the weird behaviour.
Thank you anyway for your support.

Fabio
Tags
Grid
Asked by
Gaetano
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Gaetano
Top achievements
Rank 1
Share this question
or