使用MVC在输入字段中自动建议

时间:2017-02-21 07:47:26

标签: javascript c# asp.net-mvc asp.net-mvc-4 c#-4.0

实际上,我希望在输入字段中实现自动建议。所以我的控制器中有一些结果。我附在这里的代码。

Contorller

    public ActionResult Index()
    {
        string ProfileUrl = "http://localhost:64413/api/CorporateName/CorporateDetails?pid=" + 5999 + "";
        System.Net.Http.HttpClient DashBoardclient = new System.Net.Http.HttpClient();
        DashBoardclient.BaseAddress = new Uri(ProfileUrl);
        DashBoardclient.DefaultRequestHeaders.Accept.Clear();
        DashBoardclient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage dashboardresponseMessage = DashBoardclient.GetAsync(ProfileUrl).Result;
        var DashboardData = dashboardresponseMessage.Content.ReadAsStringAsync().Result;

        JavaScriptSerializer jss = new JavaScriptSerializer();
        List<Corporate> dbresult = (List<Corporate>)jss.Deserialize(DashboardData, typeof(List<Corporate>));

        string GroupID = null;
        string CompanyName = null;

        foreach (Corporate dbresults in dbresult)
        {
            GroupID = dbresults.GroupID;
            CompanyName = dbresults.GroupName;

        }

        return View("Index", dbresult);
    }

所以,我在这里调用API并将结果存储在 dbresult .... dbresult 有两个参数(GroupId,GroupName).. 公司是模型

查看页面

@model ClinicSystemMVC.Models.Corporate
        @using (Html.BeginForm())
        {
           @Html.AntiForgeryToken()
           @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.LabelFor(model => model.GroupName)
                  <div class="col-sm-9">
                     @Html.EditorFor(model => model.GroupName, new { @class = "form-control" })
                  </div>                            
            </div>
                }

我刚刚提到了一个我期待自动建议的div。

的Javascript

<script type="text/javascript">  
$(document).ready(function () {  
    $("#GroupName").autocomplete({  
        source: function (request, response) {  
            $.ajax({  
                url: "/Home/Index",  
                type: "GET",  
                dataType: "json",  
                data: { Prefix: request.term },  
                success: function (data) {  
                    response($.map(data, function (item) {  
                        return { value: item.GroupName};  
                    }))  

                }  
            })  
        },  
        messages: {  
            noResults: "", results: ""  
        }  
    });  
})  

使用此附加代码,我无法实现自动建议。输入字段没有列出任何内容。那么如何更改我的代码以在输入字段中实现自动建议。

任何帮助表示赞赏。在此先感谢!!!

0 个答案:

没有答案