多個屬性我有一個JSON是這樣的:LINQ和JSON,匿名類型不能有相同的名稱
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = reservations.Select(x => new { x.ReservationId, x.Employee.Name,roomName = x.Room.Name, x.Date, x.StartTime, x.EndTime, x.Notes}).ToList()
.Select(x => new
{
id = x.ReservationId,
cell = new string[]
{
roomName,
x.Name,
x.Date.ToString().Split(' ')[0],
x.StartTime.ToString().Remove(5) +" - " +x.EndTime.ToString().Remove(5),
x.Notes,
String.Format("<input type=\"button\" value=\"DETAILS\" onclick=\"window.location.href='/Reservation/Details/{0}'\"/>   <input type=\"button\" value=\"EDIT\" onclick=\"window.location.href='/Reservation/Edit/{0}'\"/>   <input type=\"button\" value=\"DELETE\" onclick=\"window.location.href='/Reservation/Delete/{0}'\"/>", x.ReservationId),
}
}).ToArray(),
};
這將會返回JSON的jqGrid的。當我在第一個select語句中鍵入x.Room.Name時,由於已經存在具有相同名稱的屬性(即x.Employee.Name),因此發生錯誤。
所以我試過「roomName = x.Room.Name」,我把roomName放在單元格內容中。但是,當json返回到我的jqGrid時,該列應該包含roomName,它是空的。我怎麼能以其他方式顯示x.Room.Name的值?
瞎猜,但也許這將工作? x => new {x.ReservationId,new {x.Employee.Name},new {x.Room.Name}} – McGarnagle
可能你使用了Entity Framework,並忘記使用'Include'來填充'Room'部分(參見(例如)(http://msdn.microsoft.com/zh-cn/library/bb896272.aspx#sectionSection0))? – Oleg