Click here to Skip to main content
11,578,241 members (61,453 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC jQuery
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; }
    }
}
Posted 22-Nov-12 2:56am
Edited 22-Nov-12 3:09am
(no name)136.8K
v2

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

  Print Answers RSS
0 Richard MacCutchan 165
1 OriginalGriff 150
2 Peter Leow 150
3 Afzaal Ahmad Zeeshan 100
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100