Click here to Skip to main content
15,895,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
@{
    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
C#
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
C#
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; }
    }
}
Posted
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900