weixin_33721344 2016-06-16 06:56 采纳率: 0%
浏览 28

在MVC4中级联文本框?

Hi I have two fields in my view CustomerName and ContactPerson. Before i kept these two field as dropdown field. Now I change the customer name field as autoComplete textbox and contact person field as dropdown field.

Before i kept these two field as Cascading dropdown fields.That is if i select the CusomerName the CustomerName related ContactPerson will automatically in ContactPerson dropdown.

Now I change that customer name field to textbox and contact person field as dropdown. Now what i want is if i type and select the CustomerName in customername textox, the CustomerName related ContactPerson have to load automatically in contactperson dropdown.

My Model(VisitorsViewModel)

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

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

My View

@Html.LabelFor(model => Model.CustomerName, new { @class = "control-label" })
@Html.TextBoxFor(model => Model.CustomerName, new { @class = "form-control" })
@Html.HiddenFor(model => Model.CustomerID)


@Html.Label("Contact Person", new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerContactID, new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @class = "form-control", type = "text", id = "CustomerContactID" })

My Jquery Code

<link href="~/Areas/Sales/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Areas/Sales/Scripts/jquery-2.2.3.min.js"></script>
<script src="~/Areas/Sales/Scripts/jquery-ui.1.10.4min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
      debugger;
    $("#CustomerName").autocomplete({
      source: function (request, response) {
      $.ajax({
      url: '@Url.Action("GetVisitCustomer", "VisitorsForm")',
      datatype: "json",
      data: {
              Areas: 'Sales',
              term: request.term
            },
     success: function (data) {
     response($.map(data, function (val, item) {
     debugger;
              return {
                           label: val.Name,
                           value: val.Name,
                           customerId: val.ID
                     }                                     
                 }))
                }
             })
          },
   select: function (event, ui) {
   $("#CustomerID").val(ui.item.customerId);
      debugger;
         }
       });
     });


  var Customerid = $("CustomerID").val();
        $('#CustomerName').blur(function () {
            debugger;
            $('#CustomerContactID').empty();
            $.ajax(
                    '@Url.Action("GetContactPersobByCustomerId", "VisitorsForm", new { Area = "Sales" })', {

            type: "POST",
            datatype: "Json",
            data: { CustomerID: $('#CustomerID').val() },
            success: function (data) {
                $('#CustomerContactID').append($('<option></option>').val('').text('Please select'));
                $.each(data, function (index, value) {
                    $('#CustomerContactID').append('<option value="' + value.CustomerContactID + '">' + value.ContactReference + '</option>');
                });
            }
        });
       });

My Controller Code

 public JsonResult GetVisitCustomer(string Areas, string term = "")
    {
        var objCustomerlist = db.Customers.Where(c => c.IsDeleted == false)
                        .Where(c => c.DisplayName.ToUpper()
                        .Contains(term.ToUpper()))
                        .Select(c => new { Name = c.DisplayName, ID = c.CustomerID })
                        .Distinct().ToList();
        return Json(objCustomerlist, 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, JsonRequestBehavior.AllowGet);
   }

My above code is working if i type and select the customer name the customername related contact person is loading in contact person dropdown but what my doubt is i gave blur function in contact person dropdown

    $('#CustomerName').blur(function () {

I donno my code is correct or not however it is working. now what i want is please any one tell me the alternate or correct code for this blur function. I tried to explain my issue as per my level best any one help me to resolve this issue..

Advance Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料