如何在自动完成错误System.String []中返回字符串[]

时间:2016-10-26 10:08:36

标签: jquery asp.net asp.net-mvc jquery-ui-autocomplete

我头脑中的脚本

        var ProductNames;
    $(document).ready(function () {
        $("#inputSuccess2").autocomplete({
            source: function (request, response) {
                var texttyped = $("#inputSuccess2").val();
                $.ajax({
                    url: "@Url.Content("~/Products/FetchName")",
                    dataType: "json",
                    data: "search=" + texttyped,
                    type: "get",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        var msg = Assessments_JqueryAutoComplete.AutoComplete(request.term).value; //(ajaxpro functions)
                        response(msg.d);
                        response($.map(data, function (item) {
                            return {


                                //value: '' + item

                            }
                        }
                        ))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error: " + textStatus);
                    }
                });
            },
            minLength: 1,
            select: function (event, ui) {

                window.location = "/Product/EditSingleProduct?PrdNm=" + ui.item.value;

            }
        }
                )
    });

我的控制器是

  public  string[] FetchName(String search){
  SqlConnection SearchProduct = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());

        SearchProduct.Open();
        SqlCommand cmd = SearchProduct.CreateCommand();
        List<string> Product1 = new List<string>();
        cmd.CommandText = String.Format("(SELECT [ProductName],[ProductType] FROM [Rush].[dbo].[Product] where ProductName like '%{0}%')", search);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Product1.Add(reader.GetString(0));

        }
        SearchProduct.Close();
        return Product1.ToArray();
}

我在手动检查输出时遇到解析错误 System.String []

所以我猜它无法在该下拉列表中解析string [],因为我检查了它在数组中保存了11个项的返回值 提前谢谢

1 个答案:

答案 0 :(得分:0)

 public  JsonResult FetchName(String search){
  SqlConnection SearchProduct = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());

        SearchProduct.Open();
        SqlCommand cmd = SearchProduct.CreateCommand();
        List<string> Product1 = new List<string>();
        cmd.CommandText = String.Format("(SELECT [ProductName],[ProductType] FROM [Rush].[dbo].[Product] where ProductName like '%{0}%')", search);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Product1.Add(reader.GetString(0));

        }
        SearchProduct.Close();
        return  Json(Product1.ToArray(),JsonRequestBehavior.AllowGet);
}