Search code examples
asp.net-mvcasp.net-mvc-2teleriktelerik-mvc

Sending custom values on save and update using Telerik Grid


I have a Telerik grid in my asp.net mvc application that looks something like:

Screenshot

Now it lists all the regions in a zone selected from the list placed just above the grid. zoneid is foreign key in the grid. Now I want that when I add new region in the grid the zoneID should be taken from the list instead of what is present in zone column of the grid because that value is just to display the zone and that can also be removed from the grid as it as apparent from the list which zone the listed regions belong to.

I understand that I could have used editor templates to show this list inside the grid on edit but I prefer it to be outside the grid and provide some filtering on basis of the zone as we are likely to have many regions per zone. so my concern here, now, is how can I set ZoneID of a region (edited or newly added) equal to selected value of list that shows just above the grid control.


Solution

  • I solved this problem by hooking the onSave event of Telerik grid like

    <%
    Html.Telerkik.Grid<xyz.company>()
    .Name("name")
    .// other properties
    .Events(even=>even.onSave("onSave")
    .Render();%>
    

    Inside onSave event handler in JS I have written something like

    function onSave(e)
    {
        var data = e.values;
        data["companyID"] = $("#CompanySelectList").val();
        e.values = data;
        return true;
    }
    

    onSave event adds the companyID at companyID index of json that will be submitted to the server and modelbinder will bind it with concerning property name of model.