Javascript 层叠下拉列表在编辑期间未显示保存的值

Javascript 层叠下拉列表在编辑期间未显示保存的值,javascript,asp.net-mvc,cascadingdropdown,Javascript,Asp.net Mvc,Cascadingdropdown,我正在为我的项目使用级联下拉列表。“创建”页面工作正常,选定的值保存在数据库中。但在编辑模式下,保存的值不会显示在下拉列表中 编辑视图 <div class="form-group"> @Html.LabelFor(model => model.CatID, "Category", htmlAttributes: new { @class = "control-label col-md-2" }) <div

我正在为我的项目使用级联下拉列表。“创建”页面工作正常,选定的值保存在数据库中。但在编辑模式下,保存的值不会显示在下拉列表中

编辑视图

        <div class="form-group">
            @Html.LabelFor(model => model.CatID, "Category", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CatID", ViewBag.CatID as SelectList, "--SELECT CATEGORY--", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CatID, "*", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NocID, "Nature of Occurrence", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("NocID", new SelectList(string.Empty, "Value", "Text"), htmlAttributes: new { @class = "form-control dropdown1" })
                @Html.ValidationMessageFor(model => model.NocID, "*", new { @class = "text-danger" })
            </div>
        </div>
JAVASCRIPT

<!--Cascading Dropdown for Category and Nature of Occurrence-->
<script type="text/javascript">
$(document).ready(function () {
    //Category Dropdown Selectedchange event
    $("#CatID").change(function () {
        $("#NocID").empty();
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetNoCs")',
            dataType: 'json',
            data: { id: $("#CatID").val() },
            // Get Selected Category ID.
            success: function (nocs) {
                $("#NocID").append($('<option></option>').val('').text('--SELECT NATURE OF OCCURRENCE--'));
                $.each(nocs, function (i, noc) {
                    $("#NocID").append('<option value ="' + noc.Value + '">' + noc.Text + '</option>');
                });
            },
            error: function (ex) {
                alert('Failed to retrieve categories.' + ex);
            }
        });
        return false;
    })
});
控制器

// Json Call to get nature of occurrence
    public JsonResult GetNoCs(string id)
    {
        List<SelectListItem> nocs = new List<SelectListItem>();
        var nocList = this.Getnoc(new Guid(id));
        var nocData = nocList.Select(m => new SelectListItem()
        {
            Text = m.Title,
            Value = m.NocID.ToString(),
        });
        return Json(nocData, JsonRequestBehavior.AllowGet);
    }


    // Get Nature of Occurrence from DB by CatID
    public IList<nature_of_occurrence_ref> Getnoc(Guid CatID)
    {
        return db.nature_of_occurrence_ref.Where(m => m.CatID == CatID).ToList();
    }
任何帮助都将不胜感激。
谢谢

您的代码存在多个问题。绑定到的属性和案例中的SelectList CatID不能使用相同的名称,需要根据绑定到的值在控制器中填充这两个SelectList。看到这两个副本。很抱歉,我对使用asp.net mvc进行开发是新手,这是我的实践项目。你能告诉我正确的方法吗?谢谢。读两份副本!已经找到了这个问题的解决方案。只是花了很长时间在这里发布更新。我重构了我的代码,甚至重构了我从数据库中获取数据的方式,找到了一个更好、更干净的解决方案。谢谢你的帮助。