Function that returns other functions

3

I have three methods being called separately in the controller with JSON. Each of them returns me Id Name .

I need to call these three methods in the same function in controller . How can I join all three methods in the same function?

[HttpPost]
public ActionResult GetMessageClassByResourceByDevice(int resourceId, string deviceName)
{                
    return  Json(EventFlow.GetMessageClassByResourceByDevice(resourceId, deviceName)
        .Select(f => new { EventMessageClassId = f.Id, FullName = f.Name }));

}

[HttpPost]
public ActionResult GetMessageGroupByResourceByDevice(int resourceId, string deviceName)
{
    return Json(EventFlow.GetMessageGroupByResourceByDevice(resourceId, deviceName)
        .Select(f => new { EventMessageGroupId = f.Id, FullName = f.Name }));

}

[HttpPost]
public ActionResult GetMessageByResourceByDevice(int resourceId, string deviceName)
{
    return Json(EventFlow.GetMessageByResourceByDevice(resourceId, deviceName)
        .Select(f => new { EventMessageId = f.Id, FullName = f.Name }));
}
    
asked by anonymous 28.09.2016 / 14:30

1 answer

0

If you want to return the list of contents of the three methods returns, you can do the following:

[HttpPost]
public ActionResult GetMessageClass(int resourceId, string deviceName)
{
  var list = new List<dynamic>();

  list.Add((dynamic)GetMessageClassByResourceByDevice(resourceId, deviceName).Data);
  list.Add((dynamic)GetMessageGroupByResourceByDevice(resourceId, deviceName).Data);
  list.Add((dynamic)GetMessageByResourceByDevice(resourceId, deviceName).Data);

  return Json(list);
}

If you want to return the first of the non-null returns, you can do the following:

[HttpPost]
public ActionResult GetMessageClass(int resourceId, string deviceName)
{
  var ret = (dynamic)GetMessageClassByResourceByDevice(resourceId, deviceName).Data;
  if (ret != null) {
    ret = (dynamic)GetMessageGroupByResourceByDevice(resourceId, deviceName).Data;
  }
  else if (ret != null) {
    ret = (dynamic)GetMessageByResourceByDevice(resourceId, deviceName).Data;
  }

  return Json(ret);
}
    
19.10.2016 / 22:07