ASP.NET MVC - 级联下拉菜单未保存到数据库

时间:2017-10-06 05:02:59

标签: asp.net-mvc

我创建了一个2依赖级联下拉列表:

模型类

COUNTRIES
public int COUNTRY_ID { get; set; }
public string COUNTRY_NAME { get; set; }

STATES
public int STATE_ID { get; set; }
public Nullable<int> COUNTRY_ID { internal get; set; }
public string STATE_NAME { get; set; }

CITIES
public int CITY_ID { get; set; }
public Nullable<int> STATE_ID { internal get; set; }
public Nullable<int> COUNTRY_ID { internal get; set; }
public string CITY_NAME { get; set; }

下拉列表按预期工作。但是当点击“保存”按钮时,不会保存任何内容。

控制人:城市

    // Json Call to get state
    public JsonResult GetStates(string id)
    {
        List<SelectListItem> states = new List<SelectListItem>();
        var stateList = this.Getstate(Convert.ToInt32(id));
        var stateData = stateList.Select(m => new SelectListItem()
        {
            Text = m.STATE_NAME,
            Value = m.STATE_ID.ToString(),
        });
        return Json(stateData, JsonRequestBehavior.AllowGet);
    }

    // Get State from DB by country ID
    public IList<STATES> Getstate(int CountryId)
    {
        return _statesService.GetStates().Where(stat => stat.COUNTRY_ID == CountryId).ToList();
    }

    //

    public ActionResult Create()
    {
        ViewBag.COUNTRIES = new SelectList(_countriesService.GetCountries(), "COUNTRY_ID", "COUNTRY_NAME");
        ViewBag.STATES = new SelectList(_statesService.GetStates(), "STATE_ID", "state_NAME");
        return View(); 

    }


    [HttpPost]
    public ActionResult Create(CITIES cities)
    {
        try
        {
            IEnumerable<COUNTRIES> lstCountries = _countriesService.GetCountries(); 
            if (ModelState.IsValid)
            {
                cities.ACTION_STATUS = 0;
                cities.CREATED_DATE = DateTime.Now;
                _citiesService.AddCity(cities);
                return RedirectToAction("Index");
            }
        }
        catch
        {
            ModelState.AddModelError("", "We cannot add this Cities. Verify your data entries !");
        }
        ViewBag.COUNTRIES = new SelectList(_countriesService.GetCountries(), "COUNTRY_ID", "COUNTRY_NAME", cities.COUNTRY_ID);
        ViewBag.STATES = new SelectList(_statesService.GetStates(), "STATE_ID", "STATE_NAME", cities.STATE_ID);
        return View(cities);
    }

查看:城市

<div class=" box box-body box-primary">

@*@using (Html.BeginForm())*@
@using (Html.BeginForm("Create", "Cities", FormMethod.Post, new { @class = "form-horizontal", @enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, null, new { @class = "text-danger" })

        <div class="row .col">
            <div style="margin-top:20px" class="mainbox col-md-12 col-md-offset-0 col-sm-8 col-sm-offset-2">
                <div class="panel panel-info">
                    <div class="panel-heading">
                        <div class="panel-title">Create City</div>
                    </div>
                    <div class="panel-body">

                        <div class="col-md-4">
                            <div>
                                @Html.LabelFor(model => model.COUNTRY_ID, "Country Name", new { @class = "control-label" })
                                @Html.DropDownList("COUNTRIES", ViewBag.COUNTRIES as SelectList, "-- Please Select a Country  --", new { style = "width:250px" })
                                @Html.ValidationMessageFor(model => model.COUNTRY_ID, null, new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div>
                                @Html.LabelFor(model => model.STATE_ID, "State Name", new { @class = "control-label" })
                                @Html.DropDownList("STATES", new SelectList(string.Empty, "Value", "Text"), "-- Please select a State --", new { style = "width:250px", @class = "dropdown1" })                              
                                @Html.ValidationMessageFor(model => model.STATE_ID, null, new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div>
                                @Html.LabelFor(model => model.CITY_NAME, "City Name", new { @class = "control-label" })
                                @Html.TextBoxFor(model => model.CITY_NAME, new { @style = "border-radius:3px;", @type = "text", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.CITY_NAME), @autocomplete = "on" })
                                @Html.ValidationMessageFor(model => model.CITY_NAME, null, new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div class="panel-title">
                            <div class="form-actions no-color">
                                <input type="submit" value="Create" class="btn btn-success" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")


      <script type="text/javascript">
         $(document).ready(function () {
        //Country Dropdown Selectedchange event
        $("#COUNTRIES").change(function () {
            $("#STATES").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetStates")', // Calling json method
                dataType: 'json',
                data: { id: $("#COUNTRIES").val() },
                // Get Selected Country ID.
                success: function (states) {
                    $.each(states, function (i, state) {
                        $("#STATES").append('<option value="' + state.Value + '">' +
                             state.Text + '</option>');
                    });
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                }
            });
            return false;
        })
    });
</script>

当我点击“国家/地区”下拉列表时,它会填充并按预期加载相应的状态。当我点击“保存”按钮时,没有保存任何内容。请问是什么问题

0 个答案:

没有答案