自动完成多列MVC不显示数据

时间:2015-04-14 11:05:41

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

我已成功实现了jquery-ui的autosomplete,但我希望多列自动完成。我已经实现了太多,但现在不知道如何从数据库中获取第一个,第二个和第三个值。

   $(function () {
        //overriding jquery-ui.autocomplete .js functions
        $.ui.autocomplete.prototype._renderMenu = function (ul, items) {
            var self = this;
            //table definitions
            ul.append("<table><thead><tr><th>ID#</th><th>Name</th><th>Company&nbsp;Name</th></tr></thead><tbody></tbody></table>");
            $.each(items, function (index, item) {
                self._renderItemData(ul, ul.find("table tbody"), item);
            });
        };
        $.ui.autocomplete.prototype._renderItemData = function (ul, table, item) {
            return this._renderItem(table, item).data("ui-autocomplete-item", item);
        };
        $.ui.autocomplete.prototype._renderItem = function (table, item) {
            return $("<tr class='ui-menu-item' role='presentation'></tr>")
              .data("item.autocomplete", item)
              .append("<td >" + item.id + "</td>" + "<td>" + item.value + "</td>" + "<td>" + item.cp + "</td>")
              .appendTo(table);
        };
            //random json values
        //var projects = [
        //    { id: 1, value: "Thomas", cp: 134 },
        //    { id: 65, value: "Richard", cp: 1743 },
        //    { id: 235, value: "Harold", cp: 7342 },
        //    { id: 78, value: "Santa Maria", cp: 787 },
        //    { id: 75, value: "Gunner", cp: 788 },
        //    { id: 124, value: "Shad", cp: 124 },
        //    { id: 1233, value: "Aziz", cp: 3544 },
        //    { id: 244, value: "Buet", cp: 7847 }
        //];
        $("#search").autocomplete({
            minLength: 3,
            source: '@Url.Action("GetMedicineName")',
        })
    });
</script>   

这是我的控制器

    Function GetMedicineName(term As String) As JsonResult
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("SHCTConnectionString").ConnectionString
        Dim conn As New SqlConnection(connectionString)
        Dim medicine As New List(Of String)
        Dim medicineID As New List(Of Integer)
        Dim medicineCompanyName As New List(Of String)
        Dim sSql = "Select TOP 20 medicine_id, medicine_name, medicine_company_name, location From UM_MEDICINE_MASTER WHERE hospital_id = 1 AND medicine_name LIKE '" & term & "%'"
        Dim cmd As SqlCommand = New SqlCommand(sSql, conn)
        conn.Open()
        Dim rst As SqlDataReader = cmd.ExecuteReader
        Do While rst.Read
            medicine.Add(rst!medicine_name)
        Loop
        medicine.ToList()
        Return Json(medicine, JsonRequestBehavior.AllowGet)
    End Function

我希望ID,姓名和公司名称来自数据库的自动填充字段中的所有3个字段。调用var项目时,所有数据都正确显示,但我想要数据库中的数据

1 个答案:

答案 0 :(得分:0)

你能不能创建一个从数据库中填充的小类,然后将其作为列表返回?

public class Medicine
{
    public long id { get; set; }
    public string value { get; set; }
    public long cp { get; set; }
}

public JsonResult AutocompleteSuggestions(int conn_id, string term)
{


    List<Medicine> Values = new List<Medicine>();

    Values.Add(new Medicine {
        id = 123,
        cp = 76876,
        value = "test"
    });
    Values.Add(new Medicine {
        id = 321,
        cp = 3444,
        value = "more"
    });

    return Json(Values, JsonRequestBehavior.AllowGet);

}