如何填充基于另一个下拉列表中MVC 4选定值下拉列表? [英] How to populate a drop down list based on another drop down list selected value in MVC 4?

查看:119
本文介绍了如何填充基于另一个下拉列表中MVC 4选定值下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提到<一个href=\"http://stackoverflow.com/questions/29675636/how-to-populate-a-dropdownlist-based-on-another-dropdownlist-selected-value-in-m\">This答计算器的它工作正常,但因为我已经在采取的形式下拉列表中,所以当第二个下拉列表填充得先在下拉列表中选择的值是越来越影响。如何保持这个值?

I have referred This ans of stackoverflow It is working fine but since i have taken drop down list in forms so when second dropdown list gets populate selected value in first dropdown list is getting effected. How to maintain that value??

下面是我的code。

查看

@{ Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }); }
@Html.DropDownList("country", ViewData["Id"] as List<SelectListItem>, new { onchange = "this.form.submit();" })
@{ Html.EndForm(); }


@{ Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }); }
@Html.DropDownList("state", ViewData["Id1"] as List<SelectListItem>, new { onchange = "this.form.submit();" })
@{ Html.EndForm(); }


@{ Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }); }
@Html.DropDownList("city", ViewData["Id2"] as List<SelectListItem>, new { onchange = "this.form.submit();" })
@{ Html.EndForm(); }

控制器

 public ActionResult Create()
    {
        FillCountry();
        FillState();
        FillCity();
        return View();
    }
    [HttpPost]
    public ActionResult Create(User ur)
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "Insert into tblTest (Name,Email,MobileNo) values('" + ur.Name + "','" + ur.Email + "','" + ur.MobileNo + "')";
        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();
        TempData["msg"] = "<script>alert('Inserted Successfully');</script>";
        ModelState.Clear();
        FillCountry();
        FillState();
        FillCity();
        return View();


    }
    public void FillCountry()
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tblCountry ";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        li.Add(new SelectListItem { Text = "Select", Value = "0" });
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }

        ViewData["Id"] = li;

    }
    public void FillState()
    {
        int id = Convert.ToInt16(Request["country"]);
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tblState where cid='" + id + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        li.Add(new SelectListItem { Text = "Select", Value = "0" });
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }
        ViewData["Id1"] = li;
    }
    public void FillCity()
    {
        int id = Convert.ToInt16(Request["state"]);
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tbl_cities where StateId='" + id + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        li.Add(new SelectListItem { Text = "Select", Value = "0" });
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }
        ViewData["Id2"] = li;
    }

和还当我使用只有两个DropDownList的即ddlCountry和ddlState并选择国家从ddlCountry我ddlSates是歌厅填充正确但ddlCountry选定的国家正改变

and also when I am using only two dropdownlist i.e ddlCountry and ddlState and select Country from ddlCountry my ddlSates is geting populated properly but the selected Country from ddlCountry is getting changed

推荐答案

这篇和它的工作完美

控制器

public ActionResult Index()
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tbl_country ";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }
        ViewData["country"] = li;
        return View();
    }
    public JsonResult StateList(int Id)
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tbl_states where cid='" + Id + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }
        return Json(li, JsonRequestBehavior.AllowGet);
    }
    public JsonResult Citylist(int id)
    {
        string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        string query = "select * from tbl_cities where stateid='" + id + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        List<SelectListItem> li = new List<SelectListItem>();
        while (rdr.Read())
        {
            li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
        }
        return Json(li, JsonRequestBehavior.AllowGet);
    }

查看

@using (Html.BeginForm())
{ @Html.DropDownList("Country", ViewData["country"] as SelectList, "Select Country", new { id = "Country", style = "width: 150px;" })<br />
<select id="State" name="state" , style="width: 150px;"></select><br />
<select id="city" name="City" , style="width: 150px;"></select><br />
}
@Scripts.Render("~/bundles/jquery")
<script type="text/jscript">
$(function ()
{
    $('#Country').change(function ()
    {        
        $.getJSON('/Cascading/StateList/' + $('#Country').val(), function (data)
        {
            var items = '<option>Select State</option>';
            $.each(data, function (i, state)
            {
                items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            });
            $('#State').html(items);
        });
    });

    $('#State').change(function ()
    {
        $.getJSON('/Cascading/Citylist/' + $('#State').val(), function (data)
        {
            var items = '<option>Select City</option>';
            $.each(data, function (i, city)
            {
                items += "<option value='" + city.Value + "'>" + city.Text + "</option>";
            });
            $('#city').html(items);
        });
    });
});

这篇关于如何填充基于另一个下拉列表中MVC 4选定值下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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