C# 无法从json结果MVC 4中检索值

C# 无法从json结果MVC 4中检索值,c#,jquery,ajax,asp.net-mvc,json,C#,Jquery,Ajax,Asp.net Mvc,Json,我无法从JSON控制器操作中获得所需的结果。我在网上搜索过,但没有任何建议的解决方案可以解决我的问题 我的控制器操作: public JsonResult AutoComplete(string term) { var result = (from c in db.CategoryContents where c.Title.ToLower().Contains(term.ToLower())

我无法从JSON控制器操作中获得所需的结果。我在网上搜索过,但没有任何建议的解决方案可以解决我的问题

我的控制器操作:

public JsonResult AutoComplete(string term)
        {
            var result = (from c in db.CategoryContents
                          where c.Title.ToLower().Contains(term.ToLower())
                          select new { c.Title, c.ImageURL, Description = c.Category.Name + " Review" }).Distinct();
                            return Json(result, JsonRequestBehavior.AllowGet);
        }
这是我的jQuery ajax文件:

$(document).ready(function () {


            var displayLimit = 7;


    // jqueryui autocomplete configuration
    $("#term").autocomplete({
                    source: function (req, resp) { // get JSON object from SearchController
            $.ajax({
                url: "/Search/AutoComplete", // SearchController JsonResult
                type: "POST",
                dataType: "json",
                data: { term: req.term },
                success: function (data) {
                    resp($.map(data, function (item) {

                        return { label: item.Name, value: item.Name, imageURL: item.ImageURL, id: item.ID };
                    }

                    ));
                }
            });
        },



        select: function (event, ui) { // keyword selected; parse values and forward off to ProductController's ViewProduct View
            var selected = ui.item;
            var mdlNum, mdlName;

            if (selected.value !== null) {
                var array = selected.value.split(' ');
                mdlNum = array[0].toLowerCase();
                //   mdlName = selected.value.replace(array[0], '').trim().toLowerCase().replace(/[^a-z0-9]+/g, ' ');
                // window.location.replace('http://' + location.host + '/Search/Refine?ref=' + mdlNum + '' + mdlName);
                window.location.replace('http://' + location.host + '/Category/Details/' + ui.id);

            }

        },

        open: function () { $('ul.ui-autocomplete').addClass('opened') },
        close: function () { $('ul.ui-autocomplete').removeClass('opened').css('display', 'block'); }


    }) 

.data("ui-autocomplete")._renderItem = function (ul, item) {

            //var inner_html = '<a><div id="example" class="k-content"><div class="demo-section"><div class=".customers-list img"><img src="' + "../common/theme/images/gallery/3.jpg" + '"></div><div class="customers-list h3">' + item.label + '</div><div class="customers-list p">' + item.description + '</div></div></div></a>';


            var newText = String(item.value).replace(
                   new RegExp(this.term, "gi"),
                   "<strong>$&</strong>"
                 //  "<span class='ui-state-highlight'>$&</span>"
                   );

            var inner_html = '<a><div class="list_item_container"><div class="image"><img src="' + item.imageURL + '" alt="" /></div><div class="labels">' + newText + '</div><div class="description">' + item.id + '</div></div></a>';


            return $("<li></li>")
                .data("item.autocomplete", item).append(inner_html)
                .appendTo(ul);

        };

它说的是/Category/Details/undefined,但我想要
id
而不是
undefined
。请帮助。

您需要从自动完成返回您的
ID
,它在投影中丢失

将其改为以下内容:

var result = (from c in db.CategoryContents
                          where c.Title.ToLower().Contains(term.ToLower())
                          select new { c.Title, c.ImageURL, Description = c.Category.Name + " Review", ID = c.ID }).Distinct();
                            return Json(result, JsonRequestBehavior.AllowGet);

以上假设数据库中的
ID
为大写。

jQuery UI Autocomplete需要一个只包含
标签和
值的数据源。即使您正在向它传递额外的属性:
imageURL
id
,这些属性在
select
回调中也不可用

更改此项:

resp($.map(data, function (item) {
    return { label: item.Name, value: item.ID, imageURL: item.ImageURL, id: item.ID };
}
这是:

window.location.replace('http://' + location.host + '/Category/Details/' + ui.item.value);

我已经更改了它,因此每个建议项的
被设置为web服务中的
项.ID
。这可以通过回调中的
ui.item.value
获得。

谢谢它解决了我的问题,但是当我应用你的代码我的renderItem方法给出错误时,我已经编辑了我的jquery代码,现在你可以看到了。这是链接,先生,请帮帮我
window.location.replace('http://' + location.host + '/Category/Details/' + ui.item.value);