级联下拉列表在 mvc4 中不起作用

本文关键字:不起作用 mvc4 下拉列表 级联 | 更新日期: 2023-09-27 18:32:57

嗨,我有两个字段"客户名称"和"联系人"。 如果我选择客户名称,则依赖于客户名称的值需要存储在ContactPeson下拉列表中。根据下面的代码,它从 Db 获取数据并存储在两个下拉列表中,如果我选择"客户名称",相关的"联系人"会自动存储或显示在联系人下拉列表中。一切正常。但问题是,如果我在下拉列表中选择值并尝试将其保存在 Db 中,则不会保存该值。对于这两个字段,该值将为空。我不知道我哪里做错了。任何人都能为这个问题提供解决方案。

提前感谢..

我的观点级联下拉

我的视图模型

    public Nullable<System.Guid> CustomerID { get; set; }
    public string CustomerName { get; set; }
    public Nullable<System.Guid> CustomerContactID { get; set; }
    public string ContactPerson { get; set; }

我的观点

 <div class="col-sm-4">
            <div class="form-group">
                  @Html.LabelFor(model => model.CustomerName , new { @class = "control-label" })
   @Html.DropDownList("dropdownCustomer", new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @style = "width:250px;" })
</div>
</div>

 <div class="col-sm-4">
            <div class="form-group">
                  @Html.LabelFor(model => model.ContactPerson, new { @class = "control-label" })
        @Html.DropDownList("dropdownCustomerContact", new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @style = "width:250px;" })
            </div></div>

J 查询代码

  $(function () {
    $.ajax({
        type: "GET",
        url: "/VisitorsForm/GetCustomers",
        datatype: "Json",
        success: function (data) {
            $.each(data, function (index, value) {
                $('#dropdownCustomer').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>');
            });
        }
    });
    $('#dropdownCustomer').change(function () {
        $('#dropdownCustomerContact').empty();
        $.ajax({
            type: "POST",
            url: "/VisitorsForm/GetContactPersobByCustomerId",
            datatype: "Json",
            data: { CustomerID: $('#dropdownCustomer').val() },
            success: function (data) {
                $.each(data, function (index, value) {
                    $('#dropdownCustomerContact').append('<option value="' + value.CustomerContactID + '">' + value.ContactReference + '</option>');
                });
            }
        });
    });
});

我的控制器

 public ActionResult Create()
        {
    ViewBag.CustomerContactID = new SelectList(db.CustomerContacts, "CustomerContactID", "ContactReference");
    ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName");
        return View();
    }
 public JsonResult GetCustomers()
    {
        return Json(db.Customers.ToList(), JsonRequestBehavior.AllowGet);
    }
 public JsonResult GetContactPersobByCustomerId(string customerId)
    {
        Guid Id = Guid.Parse(customerId);

        var customercontacts = from a in db.CustomerContacts where a.CustomerID == Id select a;
        return Json(customercontacts);
    }
     [HttpPost]
    public ActionResult Create(VisitorsViewModel visitorviewmodel)
    {
   ViewBag.CustomerContactID = new SelectList(db.CustomerContacts, "CustomerContactID", "ContactReference",visitorviewmodel .CustomerContactID );
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName",visitorviewmodel .CustomerID );
  var VisitorsViewObj = new VisitorsForm()
        {
  CustomerID = visitorviewmodel.CustomerID,
  CustomerContactID = visitorviewmodel.CustomerContactID
   };

级联下拉列表在 mvc4 中不起作用

   <div class="col-sm-4">
    <div class="form-group">
@Html.LabelFor(model => model.CustomerName , new { @class = "control-  label" })
@Html.DropDownListFor(model => model.CustomerID, new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @style = "width:250px;" })
                   </div>
                   </div>

   <div class="col-sm-4">
    <div class="form-group">
@Html.LabelFor(model => model.ContactPerson, new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerContactID, new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @style = "width:250px;" })
        </div> </div>

   $(function () {
    $.ajax({
        type: "GET",
        url: "/VisitorsForm/GetCustomers",
        datatype: "Json",
        success: function (data) {
            $.each(data, function (index, value) {
                $('#CustomerID').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>');
            });
        }
    });
    $('#CustomerID').change(function () {
        $('#CustomerContactID').empty();
        $.ajax({
            type: "POST",
            url: "/VisitorsForm/GetContactPersobByCustomerId",
            datatype: "Json",
            data: { CustomerID: $('#CustomerID').val() },
            success: function (data) {
                $.each(data, function (index, value) {
                    $('#CustomerContactID').append('<option value="' + value.CustomerContactID + '">' + value.ContactReference + '</option>');
                });
            }
        });
    });
});