我是相當新的MVC和工作與Twitter的Bootstrap MVC4網站,我們需要一個HtmlHelper爲TypeAhead。MVC Bootstrap typeahead HtmlHelper
我得到的代碼爲Twitter的引導here一個的HtmlHelper:
public static MvcHtmlString TypeaheadFor<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression,
IEnumerable<string> source,
int items = 8)
{
if (expression == null)
throw new ArgumentNullException("expression");
if (source == null)
throw new ArgumentNullException("source");
var jsonString = new JavaScriptSerializer().Serialize(source);
return htmlHelper.TextBoxFor(
expression,
new {
autocomplete = "off",
data_provide = "typeahead",
data_items = items,
data_source = jsonString
}
);
}
我還寫了下面的代碼爲預輸入提供JSON:
public JsonResult Autocomplete(string input) {
var model = db.Users
.Select(g => new {
label = g.Name.StartsWith(input)
});
return Json(model, JsonRequestBehavior.AllowGet);
}
因爲它是現在,我提供帶IEnumerable的HtmlHelper < string>來填充建議框。我無法弄清楚如何使用我自己的JsonResult。它也需要是動態的,因爲我們需要網站上multipe實例的HtmlHelper,它有幾個不同的項目源,都是使用EF從數據庫中提取的。
我不喜歡這個解決方案是IEnumerable < string>的內容顯示在html源代碼中,對訪問者來說很容易讀取。
有沒有辦法讓Tweets的HtmlHelper typeahead.js和調用JsonResult作爲項目源,並且沒有將JsonResults編碼在HTML中打印到最終用戶?
只是一個簡單的參考,JSON數據也很容易通過任何Web檢查器工具(現在內置於所有瀏覽器)顯示給用戶。任何客戶端控制總是會以某種形式公開其數據。 – Terry 2013-05-07 20:41:33