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

[Solved] pass model variable to javascript function

1 Answer 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Eva
Top achievements
Rank 1
Eva asked on 16 May 2012, 08:06 PM
I have a grid which represents a collection member of the model passed into the page.  I use a custom edit function.  I need to be able to pass the ID of the model into that function.  It is passing the ID of the object selected in the grid perfectly, but the id of the model I just can't seem to get in there.  Can anyone suggest what I can do to acheive this?

@model ACS.CSG.ReviewContentManager.Models.ClientViewModel

@(Html.Telerik().Grid<ACS.CSG.ReviewContentManager.Models.ContactViewModel>()
    .Name("Contacts")
    .DataKeys(k => k.Add(o => o.ID))
    .ToolBar(cmd =>
    {
        cmd.Insert().ButtonType(GridButtonType.ImageAndText);
    })
    .DataBinding(dataBinding => dataBinding.Ajax()
        .OperationMode(GridOperationMode.Client)
        .Delete("_ContactDeleteAjax", "Client")
        .Insert("_ContactInsertAjax", "Client")
        .Update("_ContactUpdateAjax", "Client")
        .Select("_ContactsForClientAjax", "Client")
    )
    .Columns(columns =>
    {
        columns.Bound(c => c.Active)
            .Filterable(false)
            .Title(String.Empty)
            .ClientTemplate("<img alt='<#= Active ? '' : 'Inactive' #>' height='24' src='"
                + Url.Content("~/Content/Common/Images/Icons/")
                + "<#= Active ? 'in use' : 'disabled' #>.png' width='24' />")
            .Width(48);
        columns.Bound(c => c.ID);
        columns.Bound(c => c.FirstName);
        columns.Bound(c => c.LastName)
            .ClientTemplate("<#= LastName #>"
                + "<button class=\"grid-row-action-button right\" style=\"display: none\" onclick=\"ContactGrid_showRowAction(event, '<#= ID #>');\">&#9660;</button>"
                + Html.Telerik().Menu()
                    .Name("contactrowActionMenu_<#= ID #>")
                    .HtmlAttributes(new { @class = "grid-row-action-menu" })
                    .Orientation(MenuOrientation.Vertical)
                    .Items(items =>
                    {
                        items.Add()
                            .Text("Edit")
                            .ImageUrl("~/Content/Common/Images/Icons/edit.png")
                            .ImageHtmlAttributes(new { style = "width: 24px; height: 24px;" });
                        items.Add()
                            .Text("Delete")
                            .ImageUrl("~/Content/Common/Images/Icons/recycle.png")
                            .ImageHtmlAttributes(new { style = "width: 24px; height: 24px;" });
                    })
                    .ClientEvents(events => events.OnSelect("contactmenuSelect"))
            );
        columns.Bound(c => c.Phone);           
        columns.Bound(c => c.Email);
        })
    .ClientEvents(events => events
        .OnLoad("ContactGrid_onLoad")
        .OnDataBound("ContactGrid_onDataBound")
        .OnError("ContactGrid_onError")
        .OnRowSelect("ContactGrid_onRowSelect")
    )
    .ColumnContextMenu()
    .Editable(editing => editing.Enabled(true).Mode(GridEditMode.PopUp))
    .Filterable(filtering => filtering.Enabled(true))
    .Groupable(grouping => grouping.Enabled(false))
    .KeyboardNavigation()
    .Pageable(paging => paging.Enabled(false))
    .Scrollable(scrolling => scrolling.Enabled(true))
    .Selectable(selecting => selecting.Enabled(true))
    .Sortable(sorting => sorting.Enabled(true).SortMode(GridSortMode.SingleColumn))
)

THIS IS THE FUNCTION I NEED TO PASS THE MODEL ID INTO:
   function contactmenuSelect(e, id) {
        if (e.item.textContent == 'Delete') {
            $('#Contacts').data('tGrid').deleteRow($(e.currentTarget).parent().parent());
        }
        if (e.item.textContent == 'Edit') {
            $('#Contacts').data('tGrid').editRow($(e.currentTarget).parent().parent());
        }
    }


Thanks!


1 Answer, 1 is accepted

Sort by
0
Eva
Top achievements
Rank 1
answered on 18 May 2012, 05:14 PM
I figured this out.  I changed the line that calls the routine.

.Update("_ContactUpdateAjax", "Client", new { clientid = Model.ID })

Then I added the clientid to the function call:
function contactmenuSelect(e, id, clientid) {
        if (e.item.textContent == 'Delete') {
            $('#Contacts').data('tGrid').deleteRow($(e.currentTarget).parent().parent());
        }
        if (e.item.textContent == 'Edit') {
            $('#Contacts').data('tGrid').editRow($(e.currentTarget).parent().parent());
        }
    }

Works like a charm.

Tags
Grid
Asked by
Eva
Top achievements
Rank 1
Answers by
Eva
Top achievements
Rank 1
Share this question
or