如何在mvc4中填充级联下拉列表? [英] How to populate Cascaded dropdownlist in mvc4?

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

问题描述

我在DB中有两个表来填充级联下拉列表。在第一个下拉列表中更改值时,必须使用第二个表填充第二个下拉列表。我尝试在下拉列表中填充数据库数据时获取相同的数据。 countryid是状态表中的外键。州表中的第一行countryid是6,对应于国家表中的北美。它显示国家下拉列表中所有11行的相同内容。请给我正确的方向?

以下是我试过的代码。



Hi, I have two tables in DB to populate cascading dropdownlist. On change of value in first dropdown, second dropdown has to be populated using second table. I am getting same data while tried to populate database data in dropdownlist. countryid is foreign key in state table. first row countryid in state table is 6 which corresponds to north america in country table. It displays the same for all 11 rows in country dropdownlist. Please give me right direction?
Below is the code which i tried.

public ActionResult Submit()
                    {
                        List<country> allcountry= new List<country>();

                        List<state> allstate = new List<state>();
                        using (portalconnectionstring pc = new portalconnectionstring())
                        {
                            allcountry = pc.countries.OrderBy(a => a.countryname.ToList();

                        }
                        ViewBag.CountryID = new SelectList(allcountry, "countryid", "countryname");
                        ViewBag.StateID = new SelectList(allstate, "stateid", "statename");
                        return View();
                    }
            [HttpPost]
            [ValidateAntiForgeryToken] 
            public ActionResult Submit(Feedback s)
            {
                List<country> allcountry= new List<country>();
                List<state> allstate= new List<state>();

                using (portalconnectionstring pc = new portalconnectionstring())
                {
                    allcountry= pc.countries.OrderBy(a => a.countryname).ToList();
                    if (s != null && s.countryid > 0)
                    {
                        allstate= pc.states.Where(a => a.countryid.Equals(s.countryid)).OrderBy(a => a.statename).ToList();
                    }
                }

                ViewBag.CountryID = new SelectList(allcountry, "countryid", "countryname", s.countryid);
                ViewBag.StateID = new SelectList(allstate, "stateid", "statename", s.stateid);


                if (ModelState.IsValid)
                {
                    using (portalconnectionstring pc = new portalconnectionstring())
                    {
                        using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand("ddltest", conn))
                {



                        pgsqlcommand.CommandType = CommandType.StoredProcedure;

                        pgsqlcommand.Parameters.Add(new NpgsqlParameter("countryname", NpgsqlDbType.Varchar));
                        pgsqlcommand.Parameters.Add(new NpgsqlParameter("statename", NpgsqlDbType.Varchar));


                        pgsqlcommand.Parameters[0].Value = model.countryname;
                        pgsqlcommand.Parameters[1].Value = model.statename;

                        pgsqlcommand.ExecuteNonQuery();


                    }
                        ViewBag.Message = "Successfully submitted";
                    }
                }
                else
                {
                    ViewBag.Message = "Failed! Please try again";
                }
                return View(s);
            }

            [HttpGet]
            public JsonResult GetStates(string countryid = "")
            {
                List<state> allstate= new List<state>();
                int ID = 0;
                if (int.TryParse(countryid, out ID))
                {
                    using (portalconnectionstring dc = new portalconnectionstring())
                    {
                        allstate = dc.states.Where(a => a.countryid.Equals(ID)).OrderBy(a => a.statename).ToList();
                    }
                }
                if (Request.IsAjaxRequest())
                {
                    return new JsonResult
                    {
                        Data = allstate,
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    return new JsonResult
                    {
                        Data = "Not valid request",
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
            }

    }
}





cshtml:script文件





cshtml: script file

<script type="javascript">
                    $(document).ready(function () {
                        $("#countryid").change(function () {

                            var countryID = parseInt($("#countryid").val());
                            if (!isNaN(countryID)) {
                                var ddstate = $("#stateid");
                                ddstate.empty(); 
                                ddstate.append($("<option></option").val("").html("Select State"));


                                $.ajax({
                                    url: "@Url.Action("GetState","cascadedddl")",
                                    type: "GET",
                                    data: { countryID: countryid },
                                    dataType: "json",
                                    success: function (data) {
                                        $.each(data, function (i, val) {
                                            ddstate.append(
                                                    $("<option></option>").val(val.stateid).html(val.statename)
                                                );
                                        });
                                    },
                                    error: function () {
                                        alert("Error!");
                                    }
                                });
                            }
                        });
                    });
                </script>
            }

推荐答案

document )。ready(< span class =code-keyword> function (){
(document).ready(function () {


#countryid)。change( function (){

var countryID = parseInt
("#countryid").change(function () { var countryID = parseInt(


#countryid)。val());
if (!isNaN(countryID) ){
var ddstate =
("#countryid").val()); if (!isNaN(countryID)) { var ddstate =


这篇关于如何在mvc4中填充级联下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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