-2

Hi i have one view called visitors view in my project. In that view it contain two drop downs CustomerName and ContactPerson if i select the CustomerName the CustomerName related ContactPerson name will be automatically load in contact person drop down.its like Cascading DropDown. This process is working fine in normal mode that is when i run my application this cascading dropdown is working fine. But when i publish my project in LocalHost and run the application both the dropdown are not loading the values from db. These cascading is not working. so i inspect the issue in browser at that time i got these issue. The issue are below. please any one tell me what are the issue is this? Give me solution for this problem

enter image description here

My 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; }

My Controller

     public ActionResult Create()
    {
      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);
    }

My View

    <div class="col-sm-4">
      <div class="form-group">
        @Html.Label("Customer Name", new { @class = "control-label" })
        @Html.DropDownListFor(model => model.CustomerID, new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @class = "form-control required", type = "text" })
                        </div>
                    </div>

          <div class="col-sm-4">
          <div class="form-group">
         @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" })
         </div>
         </div>

My Jquery

<script>
 $(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>');
                });
            }
        });
    });
});
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
Sruthi
  • 59
  • 1
  • 2
  • 9

1 Answers1

-2

Your anonymous function should look like this:

(function($){
    $.ajax({
        //etc
    })
})(jQuery)

The way you have it $ is not in scope inside the anonymous function. It has to be passed in as a function parameter.

JimG
  • 261
  • 1
  • 5
  • 1
    Can you please explain how this random code style recommendation relates to the question? – Alexei Levenkov Feb 27 '16 at 04:47
  • The question was regarding the error circled in the image. The code posted (in the image) which is related to the line `$(function(){`. I was attempting to show the correct syntax which should be `(function($){...` which fixes the error. – JimG Feb 27 '16 at 04:49
  • 1
    @JimG `$(function(){` is correct. it is a short hand to document ready style. And the code you posted is a Closure syntax which is not related to this question – Rajshekar Reddy Feb 27 '16 at 04:55
  • @Reddy OK, thanks. I guess my thinking was off. I appreciate the feedback. – JimG Feb 27 '16 at 04:56