0
votes

I am having previously defined action which return the List<T>() int Json Format using JsonResult. I want to use the same action's JSON result into another action.

For better understanding, My Common Action is::

public JsonResult GetReferences(long CustomerID)
    {

         var CustomerReference = new List<ReferenceViewModel>();
         //Code to push data into CustomerReference
         return Json(CustomerReference.OrderBy(x=>x.ReferenceName));
    }

I want to use the above JsonResult into my other Action as::

public ActionResult MassUpdate(List<long> CustomerIDs, List<long> DocumentIDs) 
        {
                List<ReferenceViewModel> JsonCustomerReference =  GetReferences(CustomerID);
        }

But I am not able to access the JsonResult here. How can I do that?

2

2 Answers

0
votes

GetReferences is returning a JSONREsult which is not a List. why dont you try abscracting this a bit:

private IEnumerable<ReferenceViewModel> getReferences(long CustomerID)
{
   var customerReferences = new List<ReferenceViewModel>();
   //Code to push data into CustomerReference
   return customerReferences;
}

public JsonResult GetReferences(long CustomerID)
{
     return Json(customerReference(CustomerID).OrderBy(x=>x.ReferenceName));
}

public JsonResult MassUpdate(List<long> CustomerIDs, List<long> DocumentIDs)
{
    var allData = CustomerIDs.SelectMany(x => customerReference(x)).OrderBy(x=>x.ReferenceName);
    return Json(alldata);
}

I am making some assumptions here. But i think this is what you are kind of going for. you could also change the return type of getReferences to an IEnumerable and do the sorting in each Action if that is what you are going for. I updated my answer to reflect that change.

0
votes

I hope that you want something like this.

JsonResult hold data in Data Property. Which is object so you need to cast it back.

List JsonCustomerReference = (List)((JsonResult)GetReferences(CustomerID)).Data;