2

我正在使用带有MVC3的asp.net中的html5和kendo ui开发站点我遇到了一个问题,即当组合框选择的值应该传递给模型并且来自数据库的数据应该绑定到视图中的网格时如何发送值

这是视图中的代码

<h3>
    Employee Name
</h3>
<input id="comboBox1" />

<h3>
    Employee Details using remote
</h3>
<div id="empgrid">

var dataSource = new kendo.data.DataSource({
    transport: {
        read: "/Home/GetData",
        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 () {

    $("#comboBox1").kendoComboBox({
        index: 0,
        dataTextField: "ename",
        dataValueField: "eid",
        filter: "contains",
        dataSource: {
            type: "json",
            serverFiltering: true,
            serverPaging: true,
            pageSize: 5,
            transport: {
                read: "Home/GetData"
            }
        }
    });

    $("#empgrid").kendoGrid({
        pageable: true,
        toolbar: ["create", "save", "cancel"],
        editable: true,
        dataSource: dataSource,
        columns: [

                  { title: 'Employee Id', field: 'eid', width: '35%', sortable: true },
                  { title: 'Employee Name', field: 'ename', width: '45%', flex: 1, sortable: true },
                  { title: 'Age', field: 'age', width: '25%', flex: 1, sortable: true },
                  { title: 'Salary', field: 'salary', width: '45%', flex: 1, sortable: true }

                  ],
        sortable: true
    });




        $("#get").click(function () {
            var customDataListId = $("#comboBox1").data("kendoComboBox");
            $.getJSON('<%= Url.Action("PutData", "Grid")%>', { eid: ""+customDataListId.text()+"" }, function (result) {
                var customDataList = $('#empgrid');
                customDataList.empty();
                customDataList.append(result.PutData);
            });
            alert(customDataListId.text());
        });

});

在控制器中,以下代码

 public ActionResult Index()
    {
        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 PutData(string ename)
    {
        var emp = new Movie().PutData(ename);
        return Json(emp, JsonRequestBehavior.AllowGet);
    }

在模型中,以下代码

    DataTable Datatable = new DataTable();
    DataSet Dataset = new DataSet();

    public List<EmpPro> PutData(string ename)
    {
        string str = "Data Source=HARAVEER-PC\\SQLEXPRESS;Initial Catalog=Examples;Integrated Security=true";
        SqlConnection connection = new SqlConnection(str);
        SqlCommand command = connection.CreateCommand();
        command.CommandText = "select * from Emp where eid="+ename;
        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=HARAVEER-PC\\SQLEXPRESS;Initial Catalog=Examples;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; }
}

请帮帮我

谢谢并恭祝安康


4

0 回答 0