6
我通過生產的投影:轉換IQueryable的通用的JSON
var query = from book in books
select new
{
label = book.Title,
value = book.ID
};
在我的剃鬚刀頁我需要使用:
var booksArray = [{
@(json)
}];
使得產生的陣列看起來像:
label: 'c++',
value: 'c++'
}, {
label: 'java',
value: 'java'
}, {
label: 'php',
value: 'php'
}, {
label: 'coldfusion',
value: 'coldfusion'
}
我已經從幾種不同的方法非常接近 - 我可以得到一個在服務器端看起來正確的字符串,但是w母雞渲染到頁面本身,所有'
標記變爲'
。
但專注於通過JSON.net ...
最有可能的途徑實現這一目標似乎應該是:
var json = JsonConvert.ToString(query);
但扔:
Unsupported type: System.Linq.Enumerable+WhereSelectListIterator`2[Project.Entity.Book,<>f__AnonymousType3`2[System.String,System.Int32]]. Use the JsonSerializer class to get the object's JSON representation.
有什麼正確的JSON.net語法?
THX
上的「VAR JSON」線創下了運行時錯誤。不支持的類型:<> f__AnonymousType2'2 [System.String,System.String] []。我已經爲這兩個元素(ID實際上是一個整數)進行了明確的'ToString()'轉換,但沒有更改報告的錯誤。 mny thx – justSteve
@justSteve感謝您的反饋,您是對的。我已經更新了我的答案。 – nemesv
這樣做 - 高度讚賞。 – justSteve