Typeahead bootstrap将参数传递给模型为null

时间:2016-07-13 19:32:46

标签: c# jquery asp.net-mvc bootstrap-typeahead

我正在使用此plugin,在要提交的表单中创建自动填充字段。一切都好,除非我提交表单,传递给模型中控制器的字段为空。我不知道如何返回我获得的数据。

这是我的代码html:

@Html.TextBoxFor(m => m.Team, new { @type = "text", id = "team", Name = "query", @class = "form-control", placeHolder = "Team (Ej -> Barcelona)", autocomplete = "off" })

JS代码:

$('#team').typeahead({
            ajax: "/Home/AutocompleteTeam",
            responseText: [
                $('#team').val()        
            ]
        });

C#代码:

public ActionResult AutocompleteTeam(string query)
    {

        List<string> teams = new List<string>();
        List<TeamServiceModel> teamsService = teamService.ListTeamsByQuery(query);
        foreach (var team in teamsService)
        {
            if(team.Name.Equals("DEFAULT"))
            {
                continue;
            }
            else
            {
                teams.Add(team.Name);
            }             
        }
        return Json(teams, JsonRequestBehavior.AllowGet);
    }

返回我正在通过查询过滤的列表的服务正在运行。

1 个答案:

答案 0 :(得分:0)

Typeahead已经过滤结果。您可以进行ajax调用以获取所有团队(返回一个数组)并在typeahead中设置'local'字段并使用数组值。

在此处查看更多http://www.bootply.com/ljIOxm3qDi