2

我試圖使用Kendo UI自動完成控件與服務器篩選,但已遇到問題。自動完成控制空值調用控制器

儘管我的回調函數似乎從表單中提取正確的文本,但它始終將空值傳遞給控制器​​方法。我看不到任何重要的方式,我的代碼與示例代碼不同。我已經驗證了JavaScript被調用,並且正在調用所需的控制器方法。後者根本沒有收到JavaScript方法的價值。

我錯過了什麼?

.cshtml源:

@(Html.Kendo().AutoComplete() 
    .Name("CustomerIdAutocomplete") 
    .DataTextField("CustomerId") 
    .MinLength(3) 
    .HtmlAttributes(new { style = "width:250px" }) 
    .DataSource(source => { 
     source.Read(read => 
     { 
      read.Action("AutocompleteCustomer", "Autocomplete") 
       .Data("onAdditionalData"); 
     }) 
     .ServerFiltering(true); 
    }) 
) 

使用Javascript:

function onAdditionalData() { 
    return { 
     text: $("#CustomerIdAutocomplete").val() 
    }; 
} 

控制器方法:

public ActionResult AutocompleteCustomer(string term) 
{ 
    InformixRepository informixRepository = new InformixRepository(); 
    IList<AutocompleteCustomer> customers = informixRepository.GetMatchingCustomerIds(term); 
    return Json(customers, JsonRequestBehavior.AllowGet); 
} 

庫方法:

public IList<AutocompleteCustomer> GetMatchingCustomerIds(string text) 
{ 
    .... content omitted because "text" is already null at this point 
} 
+0

是自動完成的代碼你」重新使用?數據源指向'AutocompleteCustomer',而不是'GetMatchingCustomerIds' – Nicholas

+0

你能顯示你的ajax調用錯誤信息 –

+0

@Nicholas - 好。忘記了我已經將DB代碼重構爲一個存儲庫。我已經添加了實際的控制器方法 - 每次仍然爲「text」調用null。 –

回答

2

這應該修復它:

function onAdditionalData() { 
    return { 
     term: $("#CustomerIdAutocomplete").val() 
    }; 
} 

無論你在JavaScript中使用必須與您的你行動的參數,你叫term

public ActionResult AutocompleteCustomer(string term) 
{ 
    InformixRepository informixRepository = new InformixRepository(); 
    IList<AutocompleteCustomer> customers = informixRepository.GetMatchingCustomerIds(term); 
    return Json(customers, JsonRequestBehavior.AllowGet); 
} 
+0

我發誓我看了那1000次。謝謝! –