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

How to get selected data in grid and send parameter to controller action

4 Answers 547 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 13 Nov 2014, 06:43 AM
Hi all

i want to get selected row and pass the ProductID to controller action, these is my code

@model TelerikMvcApp2.Models.Products@{
    ViewBag.Title = "1";
    var ProductID = 0;
    int x = 0;
}
<h2>Index</h2><div>
    @ProductID
</div><div>
    <h4>Basic Button</h4>
    <p>
    @(Html.Kendo().Button()
        .Name("primaryTextButton")
        .HtmlAttributes( new {type = "button", @class = "k-primary" } )
        .Content("Clear"))
       
    </p>
</div>
    <div id="clientsDb">
        @(Html.Kendo().Grid<TelerikMvcApp2.Models.Products>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(c => c.ProductID).Width(140);
                columns.Bound(c => c.ProductName).Width(190);
                columns.Bound(c => c.UnitPrice);
                columns.Bound(c => c.UnitsInStock).Width(110);
            })
            .HtmlAttributes(new { style = "height: 380px;" })
            .Scrollable()
            .Groupable()
            .Sortable()
            .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
                .Sortable()
            .Selectable(selectable => selectable
                .Mode(GridSelectionMode.Single)
                .Type(GridSelectionType.Row))
            .Events(events => events.Change("onChange"))
            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("Customers_Read", "Home"))
            )
        )
      </div>    <div>        
        <img name="Pic1" alt="test" src='@Url.Action("Events", "Home", new { id = @ProductID })'/>
    </div>
       <script type="text/javascript">        function onChange()
        {
            var selectedrow = $("#grid").find("tbody tr.k-state-selected");
            var goods = $('#grid').data("kendoGrid").dataItem(selectedrow);
            var goodsjson = goods.toJSON();
            var goodsID = goodsjson.ProductID;
            document.Pic1.src = '@(Url.Action("Events", "Home", new { id= goodsID}))';
                    }</script>   

it can build successfully but when run it, it will show The name 'goodsID' does not exist in the current context 

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 Nov 2014, 08:27 AM
Hello,

You should append the parameter to the URL:
'@(Url.Action("Events", "Home"))?id=' + goodsID;
The error will be thrown because Url.Action is a server-side method and goodsID is a JavaScript variable.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Robert
Top achievements
Rank 1
answered on 19 Nov 2014, 02:53 AM
Thanks~

it works~
0
valdemar
Top achievements
Rank 1
answered on 11 Aug 2017, 07:12 PM
Si quiero hacer esta reucperación pero en jquery o javascript me encantaría.
0
Stefan
Telerik team
answered on 15 Aug 2017, 10:49 AM
Hello Valdemar,

Please have in mind that the Kendo UI official support services are only in English.

Please provide a short description of the issue in English and if possible a small runnable example and we will gladly assist.

Also, I can suggest submitting a new support ticket in our system.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Robert
Top achievements
Rank 1
valdemar
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or