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

how to bind row inforation from controller to view

1 Answer 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shankar parshimoni
Top achievements
Rank 1
shankar parshimoni asked on 22 Nov 2012, 01:52 PM

here i will explain what i am facing exactly in kendo ui mvc.

my requarement is displaying values in Grid from the database based on the comboBox selection.

here i successfully binding values from database to combobox ,as well as passing selected value to controller and view AND again retriving row information up to controller.but the problem is row details is not binding to grid (in view)..please let me know how to bind those values in view.....thanks in advance

code in Index.cshtml page

@{

    ViewBag.Title = "Home Page";

}

<h2>@ViewBag.Message</h2>

<div id="tshirt-view" class="k-header">

    <h3>

        Employee Name

    </h3>

    <input id="comboBox1" />

    <br />

    <h3>

        Employee Details

    </h3>

    <div id="empgrid">

    </div>

</div>

<script type="text/javascript">

    var cmbvalue;

    var s;

    var dataSource = new kendo.data.DataSource({

        transport: {

            read: "/Home/PutempDataValue"

            //            update: {

            //                url: "/Products/Update",

            //                type: "POST"

            //            },

            //            destroy: {

            //                url: "/Products/Destroy",

            //                type: "POST"

            //            },

            //            create: {

            //                url: "/Products/Create",

            //                type: "POST"

            //            }

        },

        schema: {

            model: {

                id: "eid",

                fields: {

                    eid: {

                        //this field will not be editable (default value is true)

                        editable: false,

                        // a defaultValue will not be assigned (default value is false)

                        nullable: true

                    },

                    ename: {

                        validation: { //set validation rules

                            required: true

                        }

                    },

                    age: {

                        //data type of the field {Number|String|Boolean} default is String

                        type: "number",

                        validation: {

                            required: true,

                            min: 25

                        }

                    },

                    salary: {

                        type: "long",

                        validation: {

                            min: 5000

                        }

                    }

                }

            }

        },

        // determines if changes will be send to the server individually or as batch

        batch: true

        //...

    });

    $(document).ready(function () {

        //comboBoxInformation

        $("#comboBox1").kendoComboBox({

            change: function (e) {

                cmbvalue = $("#comboBox1").data("kendoComboBox").text();

                //var gridvalue = $("#empgrid").data("kendoGrid");

                //alert(gridvalue);

                debugger

                $.ajax({

                    url: "Home/PutempDataValue", //   controllerName/MethodName

                    type: 'POST',

                    contentType: "application/json; charset=utf-8",

                    data: '{"cmbvalue":"' + cmbvalue + '"}',

                    dataSource: {

                        type: "json",

                        serverFiltering: true,

                        serverPaging: true,

                        pageSize: 5,

                        transport: {

                            read: {

                                url: "Home/PutempDataValue"

                            }

                        }

                    }

                });

               // gridvalue.refresh();

                // Console:debug(gridvalue);

            },

            index: 0,

            dataTextField: "ename",

            dataValueField: "eid",

            filter: "contains",

            dataSource: {

                type: "json",

                serverFiltering: true,

                serverPaging: true,

                pageSize: 5,

                transport: {

                    read: "Home/GetData"

                }

            }

        });

        //gridInformation

        s = $("#empgrid").kendoGrid({

            pageable: true,

            toolbar: ["create", "save", "cancel"],

            editable: true,

            async: false,

            //            dataSource:dataSource,

            //                        dataSource: {

            //                            type: "json",

            //                            serverFiltering: true,

            //                            serverPaging: true,

            //                            pageSize: 5,

            //                            transport: {

            //                                read: {

            //                                    url: "Home/PutempDataValue"                                 

            //                                }

            //                            }

            //                        },

            //            dataSource: {

            //                transport: {

            //                    read: {

            //                        type: "json",

            //                        contentType: "Application/json; charset=utf-8",

            //                        url: "Home/PutempDataValue"

            //                    }

            //                }

            //            },

            columns: [

                        { title: 'Age', field: 'age', width: '25%', flex: 1, sortable: true },

                        { title: 'Employee Id', field: 'eid', width: '25%', sortable: true },

                        { title: 'Employee Name', field: 'ename', width: '35%', flex: 1, sortable: true },

                        { title: 'Salary', field: 'salary', width: '35%', flex: 1, sortable: true }

                      ],

            sortable: true

        });

    });

</script>

code in Controller

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Data;

using KendoUIMvcApplication1.Models;

namespace KendoUIMvcApplication1.Controllers

{

    public class HomeController : Controller

    {

        string Value;// = string.Empty;

        public ActionResult Index()

        {

            //ViewBag.Message = "Welcome to ASP.NET MVC!";

            //Movie objMv=new Movie();

            //var products = from m in objMv.subname select m;

            ////DataTable dt = Movie.GetData();

            ////return View(dt);

            var products = new Movie().GetData();

            ViewData["products"] = products;

            return View();

        }

        [AcceptVerbs(HttpVerbs.Get)]

        public JsonResult GetData()

        {

            var emp = new Movie().GetData();

            return Json(emp, JsonRequestBehavior.AllowGet);

        }

               

        [HttpPost]

        public ActionResult PutempDataValue(string cmbvalue)

        {

            Value = cmbvalue;

            var emp = new Movie().PutData(Value);         

            return Json(emp, JsonRequestBehavior.AllowGet);

        }

        //[AcceptVerbs(HttpVerbs.Get)]

        ////[HttpPost]

        //public ActionResult PutData(string cmbvalue)

        //{

        //    var emp = new Movie().PutData(cmbvalue);

        //    return Json(emp, JsonRequestBehavior.AllowGet);

        //}

        public ActionResult About()

        {

            return View();

        }

    }

}

code in model

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data;

using System.Data.SqlClient;

namespace KendoUIMvcApplication1.Models

{

    public class Movie

    {

        DataTable Datatable = new DataTable();

        DataSet Dataset = new DataSet();

        public List<EmpPro> PutData(string Value)

        {

            string str = "Data Source=SHANKAR-PC\\SQLEXPRESS;Initial Catalog=Occumen;Integrated Security=true";

            SqlConnection connection = new SqlConnection(str);

            SqlCommand command = connection.CreateCommand();

            command.CommandText = "select * from emp where ename='" + Value + "'";

            SqlDataAdapter sda = new SqlDataAdapter();

            sda.SelectCommand = command;

            DataSet ds = new DataSet();

            try

            {

                connection.Open();

                sda.Fill(ds);

                List<EmpPro> objlist = new List<EmpPro>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                {

                    EmpPro objemp = new EmpPro();

                    objemp.eid = Convert.ToInt32(ds.Tables[0].Rows[i]["eid"]);

                    objemp.ename = ds.Tables[0].Rows[i]["ename"].ToString();

                    objemp.age = Convert.ToInt32(ds.Tables[0].Rows[i]["age"]);

                    objemp.salary = Convert.ToInt64(ds.Tables[0].Rows[i]["salary"]);

                    objlist.Add(objemp);

                }

                //return ds.Tables[0];

                return objlist;

            }

            catch

            {

                return null;

            }

            finally

            {

                if (connection.State != ConnectionState.Closed)

                    connection.Close();

            }

        }

        public List<EmpPro> GetData()

        {

            //string str = "Data Source=(local);Initial Catalog=Student;Persist Security Info=True;Integrated Security=SSPI";

            string str = "Data Source=SHANKAR-PC\\SQLEXPRESS;Initial Catalog=Occumen;Integrated Security=true";

            SqlConnection connection = new SqlConnection(str);

            SqlCommand command = connection.CreateCommand();

            command.CommandText = "select * from emp";

            SqlDataAdapter sda = new SqlDataAdapter();

            sda.SelectCommand = command;

            DataSet ds = new DataSet();

            try

            {

                connection.Open();

                sda.Fill(ds);

                List<EmpPro> objlist = new List<EmpPro>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                {

                    EmpPro objemp = new EmpPro();

                    objemp.eid = Convert.ToInt32(ds.Tables[0].Rows[i]["eid"]);

                    objemp.ename = ds.Tables[0].Rows[i]["ename"].ToString();

                    objemp.age = Convert.ToInt32(ds.Tables[0].Rows[i]["age"]);

                    objemp.salary = Convert.ToInt64(ds.Tables[0].Rows[i]["salary"]);

                    objlist.Add(objemp);

                }

                //return ds.Tables[0];

                return objlist;

            }

            catch

            {

                return null;

            }

            finally

            {

                if (connection.State != ConnectionState.Closed)

                    connection.Close();

            }

        }

    }

    public class EmpPro

    {

        public int eid { get; set; }

        public string ename { get; set; }

        public int age { set; get; }

        public long salary { set; get; }

    }

}

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 27 Nov 2012, 03:47 PM
Hi parsanamoni,

The right approach to achieve such scenario is by filtering the Grid's DataSource. Please hook up to the change event of the combo box and use the filter method to retrieve the filtered data set.
Actually we have a demo that demonstrates a similar scenario:

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
shankar parshimoni
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or