通过jquery和json绑定下拉数据 [英] Binding drop down data through jquery and json

查看:57
本文介绍了通过jquery和json绑定下拉数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我在使用jquery和json加载下拉列表时遇到错误。但是我得到的错误就像'length'是null或者不是对象。所以请检查下面的代码,找出它有什么问题。请帮我解决我的紧急要求。

Dear All,

I am getting error when loading the dropdown with jquery and json. but am getting an error like 'length' is null or not an object." So please check my below code, and find what is wrong in it. Please help me with my urgent requirement.

<script language="javascript" type="text/javascript">

        debugger;
        $(document).ready(function () {

            $.ajax({

                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "table.aspx/BindDataToDropDown",
                data: "{}",
                dataType: "Json",
                success: function (data) {
                    $.each(data.d, function (value) {
                        $("[id$='ddl_marks']").append($("<option></option>").val(value.CourseLevelid).html(value.CourseLevel));

                        alert("hellow");
                    });
                }
                                error: function ajaxError(response) {
                                    alert(response.status + ' ' + response.statusText);
                                }


            });
        });
    

    </script>


//code
   <pre lang="c#"> [WebMethod]

    public static CourseDetails[] BindDataToDropDown()
    {


        DataTable dt = new System.Data.DataTable();
        List<coursedetails> List = new List<coursedetails>();
        using (SqlConnection con = new SqlConnection( ConfigurationManager.ConnectionStrings["TESTINGUSER"].ToString()))
        {


            using (SqlCommand cmd = new SqlCommand("Select CourseLevelid,CourseLevel from CourseLevels", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                    CourseDetails course = new CourseDetails();
                    course.CourseLevelID = Convert.ToInt32(dt.Rows[0]["CourseLevelid"]);
                    course.CourseLevel = dt.Rows[0]["CourseLevel"].ToString();
                    List.Add(course);
                }
            }

        }

        return List.ToArray();


    }
    public class CourseDetails
    {
        public int CourseLevelID { get; set; }
        public string CourseLevel { get; set; }
    }

< / coursedetails>< / coursedetails>

</coursedetails></coursedetails>

推荐答案

(document).ready(function(){
(document).ready(function () {


.ajax({

type: POST
contentType: application / json; charset = utf-8
url: table.aspx / BindDataToDropDown
data : {}
dataType: Json
成功:函数(数据){
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "table.aspx/BindDataToDropDown", data: "{}", dataType: "Json", success: function (data) {


.each(data.d ,function( value ){
.each(data.d, function (value) {


这篇关于通过jquery和json绑定下拉数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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