Action方法无法调用

时间:2013-07-26 14:01:32

标签: jquery asp.net-mvc-4 twitter-bootstrap bootstrap-typeahead

我正在尝试使用bootstrap typeahead,但无法让它调用我的Action(ASP.NET MVC 4)。以下内容在浏览器中呈现:

<div class ="control-group">
<div class="controls">
    <input autocomplete="off" data-provide="typeahead" data-url="/GetPets" id="Pets" type="text" />
</div>
   </div>

<script type="text/javascript">
    $(function () {
        $('[data-provide=typeahead]').each(function () {
            var self = $(this);
            self.typeahead({
                source: function (term, process) {
                    var url = self.data('url');

                    return $.getJSON(url, { term: term }, function (data) {
                        return process(data);
                    });
                }
            });
        });
    });

我的控制器方法看起来像

    [HttpGet]
    public JsonResult GetPets(string term)
    {
        // get data from DB. Here for simplicity sake I just make data up
        var nameList = new List<string>
{
    "Dog", "Cat", "Ant", "Alligator", "Beaver", "Camel", "Clam", "Dragonfly", "Goat"
};

        var results = nameList.Where(n => n.ToLower().Contains(term.ToLower()));

        return new JsonResult()
        {
            Data = results.ToArray(),
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

1 个答案:

答案 0 :(得分:0)

这似乎是一个网址路由问题。您正在使用“/ GetPets”,但可能需要使用“/ controllerName / GetPets”。作为建议,尝试使用[HttpPost]来执行返回Json的操作,并在客户端使用$ .post。