本文介绍了'System.Dynamic.DynamicObject'不包含'var2'RSS的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要动态填充下拉列表。有区{table_id,distrint_name}和Thana {thana_id,thana_name,district_id(fk)}的表格。



控制器代码:



want to dynamically populate dropdownlist. have table of District{district_id,distrint_name} and Thana{thana_id,thana_name,district_id(fk)}.

The controller code:

public class CenterController : Controller
         {

           string connection =ConfigurationManager.ConnectionStrings["CenterConnString"].ConnectionString;
          List<SelectListItem> districtList = new List<SelectListItem>();      
          List<SelectListItem> thanaList = new List<SelectListItem>();

public ActionResult Index()
{
    ViewBag.var1 = DistrictList();
    ViewBag.var2 = thanaList;


    return View();
}
private SelectList DistrictList()  
{
    using (SqlConnection conn = new SqlConnection(connection))
    {
        conn.Open();
        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand("select distrint_name,district_id from tbl_district ", conn);
        myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            districtList.Add(new SelectListItem { Text = myReader["distrint_name"].ToString(), Value = myReader["district_id"].ToString() });
        }
    }
    return new SelectList(districtList, "Value", "Text", "id"); //return the list objects in json form
}



public JsonResult ThanaList(int id)  
{
    using (SqlConnection conn = new SqlConnection(connection))
    {
        conn.Open();
        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand("select * from tbl_thana where district_id ='" + id + "' ", conn);
        myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            thanaList.Add(new SelectListItem { Text = myReader["thana_name"].ToString(), Value = myReader["thana_id"].ToString() });
        }
    }
    return Json(thanaList, JsonRequestBehavior.AllowGet); //return the list objects in json form
}


View code:

<pre lang="HTML">


               @{
    ViewBag.Title = "Index";
}

<script src="~/Scripts/modernizr-2.6.2.js"></script>

<script src="~/Scripts/jquery-2.1.3.min.js"></script>

<h2>Index</h2>

District          @Html.DropDownList("var1", "Choose District")
Thana             @Html.DropDownList("var2", "Choose Thana")
                                   @*dropdown with name var1, var2,var3 and with viewbag object*@
<script type="text/javascript">
    //initial var2, var3 are empty
    //dropdownlist name and viewbag object name must be same

    $(function () {
        $("#var1").change(function () {
            var name = $("#var1 :selected").val();  //if user select the tournament
            var url = 'Home/DistrictList';
            var data1 = { "id": name };
            $.post(url, data1, function (data) { //ajax call
                var items = [];
                items.push("<option value=" + 0 + ">" + "Choose Thana" + "</option>"); //first item
                for (var i = 0; i < data.length; i++) {
                    items.push("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");
                } //all data from the team table push into array
                $("#var2").html(items.join(' '));
            }); //array object bind to dropdown list
        });
});

</script>

<input type="submit" value="submit" />











问题是区列表已填充好。但是第二个ddl没有绑定任何东西。通过调试可以看出代码没有通过Viewbag.var2 = thanalist运行。






The problem is that District list is populated ok.But the second ddl is not binding anything. By debugging it is seen that the code does not run through "Viewbag.var2=thanalist".

推荐答案




这篇关于'System.Dynamic.DynamicObject'不包含'var2'RSS的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:16