2014-10-02 170 views
0

我使用下面的JavaScript函数来填充Datalist填充数据

function GetDropDownData(f) { 
    $.ajax({ 
     url: '/Rentals/Base/GetContactsForFacility?selectedFacility=' + f, 
     data: { facility: f }, 
     dataType: 'json', 
     success: function (result) { 
      response($.map(result, function (item) { 

       $('#custServiceContactsSelection').append($("<option  />").val(item.ContactName).text(item.ContactName)); 

      })); 
     }, 

     cache: false, 
     error: function (jqXHR, textStatus, errorThrown) { 
      if (errorThrown.indexOf("Your session has timed out") != -1) { 
       location.href = "/Rentals/Base/Timeout"; 
      } 
     } 
    }); 
} 

以下是我的控制器内的方法:

public ActionResult GetContactsForFacility (string selectedFacility) 
    { 
     var facilityId = new Guid(selectedFacility); 

     if (Request.IsAjaxRequest()) 
     { 
      var contacts = SessionService.AllCustomerServiceContactsForFacility(CrmService, facilityId); 


      return Json(contacts.Select(x => new { label = x.ContactName }), JsonRequestBehavior.AllowGet); 
     } 
     return Content(string.Empty); 
    } 

当我试图运行这一点,从Controller返回。但是,之后,我在我的VS中出现错误: JavaScript runtime error: 'response' is undefined

我认为,函数GetDropDownData()中缺少某些内容,但无法弄清楚究竟是什么。

请问你能指导我吗?谢谢!

回答

1

在你的AJAX请求,你需要将其更改为:

success: function (response) { 
      $.map(response, function (item) { 

       $('#custServiceContactsSelection').append($("<option  />").val(item.ContactName).text(item.ContactName)); 

      }); 
     }, 
     // rest of code 
+0

感谢您的答复!至少,错误已经消失。但是,我仍然无法看到数据主义者。让我得到这个工作,我会让你知道(并将此标记为答案!)。 :) – Vikram 2014-10-02 16:00:33

+0

这是关于同样的事情。我无法在用户界面中看到数据专家。它说“验证(XHTML 1.0过渡):Element'datalist'不被支持'。那么,有没有其他的替代方案可以使用,它类似于'datalist'并捕获发送的响应? – Vikram 2014-10-02 17:31:19