2017-05-31 44 views
0

嗨,我做了兩個下拉列表。第二個下拉列表取決於第一個下拉列表選擇的選擇。例如:如何在ViewBag中返回值以及JsonResult中的其他值ajax調用

enter image description here

從現在呼喚自然選擇值後,話單下拉列表得到由AJAX調用填寫。

我的查詢是我想只是文本話單話單(10),其中10是話單下拉列表的計數值後顯示呼叫門票下拉列表的計數值。

我的控制器是這樣的:

public JsonResult GetTickets(string id) 
    { 
     if (id == null) 
     { 
      id = "0"; 
     } 

     int CallNatureID = Convert.ToInt32(id); 

     var TicketsList = (from a in dataContext.CallTickets 
          join b in dataContext.Calls_HelpDesk 
          on a.CallTicketNumber equals b.TicketNumber 
          where a.CallNatureId == CallNatureID 
          && a.IsOpenFlag == true 
          && a.CallStatusId == 1 
          && a.TicketOwner == 1 
          && b.FollowupReasonId == 9 
         select new { a.CallTicketNumber }).ToList(); 

     int TicketCount = TicketsList.Count; 
     ViewBag.HiddenTicketCount = string.Format("({0})", TicketCount); 
     return Json(new SelectList(TicketsList, "CallTicketNumber", "CallTicketNumber"), ViewBag.HiddenTicketCount); 
    } 

ViewBag.HiddenTicketCount持有話單數的值。但我無法與

 return Json(new SelectList(TicketsList, "CallTicketNumber", "CallTicketNumber"), ViewBag.HiddenTicketCount); 
    } 

返回它如何可以返回使用JSON的ViewBag.HiddenTicketCount/TicketCount價值?

+0

簡單的解決方案是,將其存儲在一個變量中並將其返回。 –

+0

在我的最後一行返回Json()我怎麼能通過該變量? –

+1

首先返回一個匿名對象的集合,它只包含值和文本(當你不需要它時,沒有必要將'SelectListItem'的附加屬性發送到視圖中,不能使用'ViewBag' - 只是發送和附加對象 - 'return Json(new {options = TicketsList,count = TicketsList.Count});' –

回答

3

您需要修改這樣

int TicketCount = TicketsList.Count; 
return Json(new { TicketList = new SelectList(TicketsList, "CallTicketNumber", "CallTicketNumber"), Count = TicketCount }); 

而在阿賈克斯

$.ajax({ 
    //.... 
    success: function(data) { 
     var ticketList = data.TicketList;// bind dropdown using this list 
     var count = data.Count; 
     // I suppose `lblCallTicket` is your call ticket label id 
     $('#lblCallTicket').text('Call Ticket (' + count + ')'); 

    } 
}) 

控制器如果你不想從控制器發送然後簡單地做到這一點在阿賈克斯

success: function(data) { 
     var ticketList = data.TicketList;// bind dropdown using this list 
     var count = ticketList.Length; 
     // I suppose `lblCallTicket` is your call ticket label id 
     $('#lblCallTicket').text('Call Ticket (' + count + ')'); 

    } 
相關問題