匿名函数错误和什么是匿名函数?

问题描述:

嗨,我有一个视图叫做我的项目中的访问者视图。在该视图它包含两个下拉菜单客户名称ContactPerson如果我选择的客户名称相关ContactPerson名会自动在联系人下降down.its像层叠下拉加载客户名称。这个过程在正常模式下工作正常,当我运行我的应用程序这个级联下拉工作正常。但是,当我在LocalHost中发布我的项目并运行应用程序时,下拉列表不会从db中加载值。这些级联不起作用。所以我检查当时在浏览器中的问题,我得到了这些问题。这个问题在下面。请任何人告诉我这是什么问题?给我解决了这个问题匿名函数错误和什么是匿名函数?

enter image description here

我的视图模型

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

我的控制器

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

我查看

<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> 

我的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>'); 
       }); 
      } 
     }); 
    }); 
}); 
+1

未找到错误404 ..与匿名函数无关。我认为你尝试击中的URL是不正确的。是的,我同意它在你的开发中有效,但不会在你发布之后。在上面的错误中点击'GET'后的链接,你将能够看到实际的错误 –

+0

你需要发布你的代码,而不是它的图像! –

+0

好吧,我会发布我的代码等待 – Sruthi

您的匿名函数应该是这样的:

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

你有它$的方式是不是在匿名函数内部的范围。它必须作为函数参数传入。

+1

能否请你解释一下这个随机码风格推荐涉及到的问题? –

+0

问题是关于图像中圈出的错误。与$'(function(){')行相关的代码被发布(在图像中)。我试图显示正确的语法,它应该是'(function($){...',它修复了错误。 – JimG

+1

@JimG'$(function(){'是正确的。 。你发布的代码是一个与这个问题无关的Closure语法 –