繁体   English   中英

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

[英]Cascading Dropdown is not working in mvc4?

嗨,我在视图中有两个字段“ CustomerName”“ ContactPerson” 如果我选择的客户名称是依赖于客户名称值是需要存储在ContactPeson下拉。 按照下面的代码,它从Db中获取数据并存储在两个下拉列表中,并且如果我选择“ CustomerName”,则相关的“ ContactPerson”将自动存储或显示在ContactPerson Dropdown中。 一切都很好。 但是问题是如果我在下拉列表中选择值,然后尝试将其保存在Db中而不保存该值。 这两个字段的值都将为空。 我不知道我犯错的地方。 任何人都可以解决这个问题。

提前谢谢..

我的看法CascadingDropDown

我的ViewModel

    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
   };
   <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>');
                });
            }
        });
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM