如何从MVC3中的视图将值传递给控制器 [英] How to Pass value to controller from view in MVC3

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

问题描述

大家好,

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

这是视图中的代码

Hi All,

I''m developing site using html5 and kendo ui in asp.net with MVC3 i have got a problem that how to send value when combobox selected value should pass to model and the data from Database should bind to the grid in the view

here is the code in the view

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

    <div id="options">*@
    <h3>
        Employee Name
    </h3>
    <input id="comboBox1" />
    
    <h3>
        Employee Details using remote
    </h3>
    <div id="empgrid">
    
    
</div>
<style scoped>
    #example h2
    {
        font-weight: normal;
    }
    #tshirt-view
    {
        border-radius: 10px 10px 10px 10px;
        border-style: solid;
        border-width: 1px;
        overflow: hidden;
        width: 500px;
        margin: 30px auto;
        padding: 20px 20px 0 20px;
    }
    #tshirt
    {
        float: left;
        margin: 30px 40px 30px 20px;
    }
    #options
    {
        padding: 30px;
    }
    #options h3
    {
        font-size: 1em;
        font-weight: bold;
        margin: 25px 0 8px 0;
    }
    #get
    {
        margin-top: 25px;
    }
    
    .k-readonly
    {
        color: gray;
    }
</style>
    
<script type="text/javascript">

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


</script>



在Controller中,以下代码



In the Controller the following code

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




在模型中,以下代码




In the Model the following code

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

    }



please can anyone help me out

thanks and regards



please can anyone help me out

thanks and regards

推荐答案

(document).ready(function () {
(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" } } });
("#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"], 可正确, 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 });
("#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 });


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

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