2012-01-10 93 views
1

我知道這個網站上有幾個相同的問題,但即使他們提供瞭解決方案,我也找不到它。我正在嘗試實現jQuery UI自動完成功能來獲取搜索功能。ASP MVC vs jQuery UI自動完成

我的控制器:

public JsonResult search(int maxRows, string name_startsWith) 
    { 
     DataContext db = new DataContext(); 
     var result = (from p in db.Users where p.UserName.Contains(name_startsWith) || p.FirstName.Contains(name_startsWith) || p.LastName.Contains(name_startsWith) orderby p.LastName select p).Distinct().Take(maxRows).ToList(); 
     return Json(result); 
    } 

我的看法是:

$("#search").catcomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: "/h/search", 
      dataType: "json", 
      data: { 
       maxRows: 15, 
       name_startsWith: request.term 
      }, 
      success: function (data) { 
       response($.map(data, function (item) { 
        return { 
         label: item.UserName, 
         value: item.UserId, 
         categoty: "People" 
        } 
       })); 
      } 
     }); 
    } 
}); 

所以,你可以理解;我正在嘗試實施類別版本。我很擔心這個價值項目。我使用Guid作爲UserId。

在此先感謝您的幫助。

EDIT

我通過改變控制取得了一些進展,並查看如下。

控制:

[HttpPost]  
public JsonResult search(int maxRows, string name_startsWith) 
    { 
     DataContext db = new DataContext(); 
     var result = new List<User>(); 
     result = (from p in db.Users where p.UserName.Contains(name_startsWith) || p.FirstName.Contains(name_startsWith) || p.LastName.Contains(name_startsWith) orderby p.LastName select p).Take(maxRows).ToList(); 
     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

查看:

$("#search").catcomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       type: "POST", 
       cache: false, 
       dataType: "json", 
       url: '@Url.Action("search", "h")', 
       data: { maxRows: 15, name_startsWith: request.term }, 
       complete: function (data) { 
        response($.map(data, function (item) { 
         return { 
          label: item.UserName, 
          category: 'People' 
         } 
        })); 
       } 
      }); 
     } 
    }); 

現在的問題是:當 期待的 「成功」 的行爲:它不經常使用。 但是,當我選擇「完整」的行爲:現在它總是失敗,但沒有價值?

控制器似乎發佈值[count = 1],但json沒有收到它!?

解決

[HttpPost] 
    public JsonResult search(int maxRows, string name_startsWith) 
    { 
     DataContext db = new DataContext(); 
     var result = new List<User>(); 
     result = (from p in db.Users where p.UserName.Contains(name_startsWith) || p.FirstName.Contains(name_startsWith) || p.LastName.Contains(name_startsWith) orderby p.LastName select p).Take(maxRows).ToList(); 
     var viewModel = result.Select(x => new 
     { 
      value = x.UserName, 
      label = x.FirstName + " " + x.LastName, 
      category = "People" 
     }); 
     return Json(viewModel, JsonRequestBehavior.AllowGet); 
    } 

我通過發送之前打包在一個視圖模型的necesessary項目解決了這個問題。我認爲模型中的一些不可比較的東西是與json導致這種衝突(500內部服務器錯誤)。 (解決方法發現在這個職位:https://stackoverflow.com/a/8027027/1062284謝謝Darin!)

+0

我也試圖給硬編碼的標籤,值和類別名稱,但它在這種情況下也不起作用。標籤:「測試」, value:「2」, 類別:「人物」。但是當我嘗試給出一個數組作爲源;它工作沒有任何問題。 – MrGorki 2012-01-10 11:10:55

+0

你可以刪除'var result = new List ();'並將它替換爲'List result';因爲你正在創建一個新的實例。 – ivowiblo 2012-01-10 12:57:33

回答

0

嘗試在Firefox中使用FireBug將斷點放在完整的事件,並檢查從控制器收到的數據。

只有當您獲得HTTP 200結果而不是服務器返回錯誤時,成功行爲才起作用。

如果您在Firebug中使用Javascript調試器和/或Net選項卡沒有找到答案,那麼可以將http調用的響應放到您的問題中,也許我可以看到問題。

+0

感謝您的回答。我嘗試過,但無法達到,因爲結果是一個空的對象。 Ayhow我找到了合適的解決方案atlast :)無論如何謝謝你 – MrGorki 2012-01-10 13:39:09