如何将行信息从控制器绑定到视图 [英] how to bind row inforation from controller to view

查看:61
本文介绍了如何将行信息从控制器绑定到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我将解释我在kendo ui mvc中确切面临的问题.
我的资源正在根据comboBox选择在数据库中的Grid中显示值.
在这里,我成功地将值从数据库绑定到组合框,以及将选定的值传递到控制器并查看并再次检索行信息直至控制器.但是问题是行详细信息未绑定到网格(在视图中)..请让我知道如何在视图中绑定这些值.....在此先感谢

Index.cshtml页面中的代码

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; }
    }
}

推荐答案

(document).ready(function(){ //comboBoxInformation
(document).ready(function () { //comboBoxInformation


(#comboBox1").kendoComboBox({ 更改:功能(e){ cmbvalue =
("#comboBox1").kendoComboBox({ change: function (e) { cmbvalue =


(#comboBox1").data("kendoComboBox").text(); //var gridvalue =
("#comboBox1").data("kendoComboBox").text(); //var gridvalue =


这篇关于如何将行信息从控制器绑定到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆