示例#1
0
        public virtual async Task <JsonResult> SearchSuggestion(string query)
        {
            var result = await _searchEngine.AutoCompleteAsync(query, 0, 20);

            if (result.HasError)
            {
                var errorCode = ErrorLog.GetDefault(System.Web.HttpContext.Current)
                                .Log(new Error(result.Error, System.Web.HttpContext.Current));
                ViewBag.ErrorCode = errorCode;
                return(Json("", JsonRequestBehavior.AllowGet));
            }

            var currentUser = UserManager.FindById(User.Identity.GetUserId());
            var posts       = _postService.GetItemsById(result.Documents.Select(p => p.DocumentId).Take(10).ToList());
            var model       = _postModelFactory.PreparePostCardViewModel(posts, currentUser, Url);

            return(Json(model.Select(p => new { value = p.Title, data = p.PostUrl }).ToList(),
                        JsonRequestBehavior.AllowGet));
        }