使用ADO.NET在ASP.NET MVC核心中进行级联下拉

时间:2018-07-05 08:35:34

标签: javascript jquery asp.net-core asp.net-core-mvc-2.1

我正在使用存储过程使用ADO.NET从asp.net MVC核心中的国家/地区到国家进行级联下拉。在ASP.NET MVC Core中使用ADO.NET是正确的方法吗?我的问题是,从下拉列表中选择国家/地区列表时遇到问题。状态不会下降,因为CountryId没有传递到Controller方法的getStateByCountry中。建议高度赞赏。

<code>**View Model** : 
public class Country
    {
        public int CountryId { get; set; }
        public string CountryName { get; set; }
        public virtual int StateId { get; set; }
        public virtual string StateName { get; set; }
        public virtual List<State> states { get; set; }
    }
**Controller:** 
   public class CascadeController : Controller
    {
        DataBaseSetting db = new DataBaseSetting();

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Create()
        {
            try
            {
                List<Country> list_country = new List<Country>();
                List<SelectListItem> selectListItems = new List<SelectListItem>();
                list_country = db.GetCountry();
                ViewBag.Country = list_country;
                return View();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public JsonResult getStateByCountry(int ID)
        {
            List<State> Liststates = new List<State>();
            Liststates = db.GetStatesByCountry(ID);

            Liststates.Insert(0, new State { StateId = 0, StateName = "Select State" });
            return Json(new SelectList("StateId", "StateName"));    
        }
    }
**Ado Code:** 
 public class DataBaseSetting
    {
        string ConnectionString = "Data Source=ABC-17;Initial Catalog=*****User ID=sa; Password=******";
        public List<Country> GetCountry()
        {
            List<Country> list_Country = new List<Country>();
            using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
            {
                using (SqlCommand sqlCommand = new SqlCommand("spGetCountry", sqlConnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlConnection.Open();
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        Country country = new Country()
                        {
                            CountryId = (int)sqlDataReader["CountryId"],
                            CountryName = sqlDataReader["CountryName"].ToString()
                        };
                        list_Country.Add(country);
                    }
                }
                return list_Country;
            }
        }
        public List<State> GetStatesByCountry(int CountryId)
        {
            List<State> ListState = new List<State>();
            using (SqlConnection sqlCOnnection = new SqlConnection(ConnectionString))
            {
                using (SqlCommand sqlCommand = new SqlCommand("GetStateByCountry", sqlCOnnection))
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.Parameters.AddWithValue("@CountryId", CountryId);
                    sqlCOnnection.Open();
                    SqlDataReader dataReader = sqlCommand.ExecuteReader();
                    while (dataReader.Read())
                    {
                        State state = new State()
                        {
                            StateId = (int)dataReader["StateId"],
                            StateName = dataReader["StateName"].ToString(),
                        };
                        ListState.Add(state);
                    }
                }
            }
            return ListState;
        }
    }
**Create View:** 
@model CascadingDropDown.Models.Country
@{
    ViewData["Title"] = "Create";
}
<div class="row">
        <div class="form-group col-md-4">
            <label asp-for="CountryName" class="control-label"></label>
            <select asp-for="CountryId" class="form-control" asp-items="@(new SelectList(ViewBag.Country,"CountryId","CountryName"))">
                <option>Select Country</option>
            </select>
        </div>
        <div class="form-group col-md-4">
            <label asp-for="StateName" class="control-label"></label>
            <select asp-for="StateId" class="form-control" asp-items="@(new SelectList(string.Empty,"StateId","StateName"))">
                <option>Select Country</option>
            </select>
        </div>
        <div class="pull-right">
            <input type="submit" value="Register" class="btn btn-primary" />
        </div>
</div>}
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
    $(function () { 
        $("#CountryId").change(function () { 
            var url = '@Url.Content("~/")' + 'Cascade/getStateByCountry';
            alert(url);
            var ddlsource = "#CountryId";
            $.getJSON(url, { StateId: $(ddlsource).val() }, function (data) {
                var items = '';
                $("#StateId").empty();
                $.each(data, function (i, row) {
                    items += "<option value='" + row.value + "'>" + row.text + "</option>";
                    alert(ddlsource.value)
                });
                    $("#StateId").html(items);
            });
        });
    });
</script>
</code>

0 个答案:

没有答案