如何在ViewBag中返回一个值以及JsonResult ajax调用中的其他值

时间:2017-05-31 05:59:59

标签: jquery ajax asp.net-mvc

您好我已经制作了两个下拉列表。第二个下拉列表取决于第一个下拉列表选择的选择。例如:

enter image description here

现在,在从Call Nature中选择值后,Call Ticket下拉列表将被ajax调用填充。

我的查询我想在文字致电故障单之后显示致电故障单下拉列表的计数值,例如致电故障单(10) 10是Call Ticket下拉列表的计数值。

我的控制器就像:

    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保存Call Ticket计数的值。但是我无法用

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

如何使用Json返回ViewBag.HiddenTicketCount / TicketCount值?

1 个答案:

答案 0 :(得分:3)

您需要像这样修改控制器

 //click event for your button   
 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String time = returndate();

            //set your text to textView
            test.setText(time);
        }
    });
}

在Ajax中

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

如果你不想从控制器发送,那么只需在ajax中执行此操作

$.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 + ')');

   }
})