文本框上的自动完成功能恢复404错误,未找到控制器/操作

时间:2016-04-26 12:02:05

标签: jquery asp.net-mvc

我试图在autocomplete上整合textbox。但它没有按action方法从模型中获取数据。它在控制台中抛出以下错误:

  

http://localhost:56393/Api/AutoCompleteChannelName 404(未找到)

以下是.cshtml代码:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

JavaScript的:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

$(document).ready(function () {

    $("#channelName").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Api/AutoCompleteChannelName",
                type: "POST",
                dataType: "json",
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.ChannelName,

                        };
                    }))
                }
            })
        },

        select: function (event, ui) {
            event.preventDefault();
            $("#channelName").val(ui.item.label);
            $("#hdnChannelName").val(ui.item.label);
        },

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

输入标记:

<input id="channelName" class="form-control" value="" name="channelName" placeholder="Enter Channel Name To Filter" />

ActionResult:

public JsonResult AutoCompleteChannelName(string term)
    {

        List<DIM_CHANNEL> obj = bc.DIM_CHANNEL.Where(m => m.CHANNEL_NAME.ToLower().Contains(term.ToLower())).Distinct().AsEnumerable().Select(i => new DIM_CHANNEL
        {
            ChannelName = i.CHANNEL_NAME,
            ChannelKey = i.CHANNEL_KEY,
        }).ToList();

        return Json(obj, JsonRequestBehavior.AllowGet);
    }

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

将您的ajax调用类型更改为GET

$.ajax({
            url: "/Api/AutoCompleteChannelName",
            type: "GET",
            dataType: "json",
            data: { term: request.term },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.ChannelName,

                    };
                }))
            }
        })

答案 1 :(得分:1)

首先,将private static final XAPKFile[] xAPKS = { new XAPKFile(true, Constants.MAIN_OBB_VERSION, Constants.EXPANSION_FILE_SIZE) // right here expansion file size must match exactly. }; //in Constants class public static final int EXPANSION_FILE_SIZE = 285516838; //expansion file size in bytes 更改为POST。此外,您是否在GET中定义了WebAPI路线?

如果有,您需要删除该路由,或将控制器重命名为WebApiConfig.cs以外的任何其他内容

答案 2 :(得分:-1)

你错过了

[HttpGet]
你的jsonresult方法中的

属性。

请参考以下代码,希望它适合您:

    [HttpGet]
    public JsonResult AutoCompleteChannelName(string term)
        {

            List<DIM_CHANNEL> obj = bc.DIM_CHANNEL.Where(m => m.CHANNEL_NAME.ToLower().Contains(term.ToLower())).Distinct().AsEnumerable().Select(i => new DIM_CHANNEL
            {
                ChannelName = i.CHANNEL_NAME,
                ChannelKey = i.CHANNEL_KEY,
            }).ToList();

            return Json(obj, JsonRequestBehavior.AllowGet);
        }

由于