Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to be able to display the ViewBag on the view on button click event, this is my code:

[HttpPost]
public ActionResult SpecificWorkflowReport(Report2ListViewModel wf)
{

      var getSpRecord = db.Mworkflow().ToList();

      var getRecord = (from u in getSpRecord
       select new Report2ListViewModel
          {
            WorkFlowType = u.WorkFlowType,
            WorkflowInstanceId = u.WorkflowInst,
            WorkFlowDescription = u.WorkFlowDesc,
          }).ToList();

          ViewBag.WorkflowType = wf.WorkFlowType;
          ViewBag.WorkflowInstanceId = wf.WorkflowInst;
          ViewBag.WorkFlowDescription = wf.WorkFlowDesc

          var data = Newtonsoft.Json.JsonConvert.SerializeObject(getRecord);
          return Json(data);
}


What I have tried:

Worflow Type: @ViewBag.WorkflowType
Workflow Instance Id: @ViewBag.WorkflowInstanceId
Workflow Description: @ViewBag.WorkFlowDescription


the ViewBag values are always null on the view, any assistance will be appreciated.
Posted
Updated 20-Mar-18 23:48pm
Comments
F-ES Sitecore 21-Mar-18 6:40am    
You're returning json so I assume this method is called via ajax, if that is the case the ViewBag doesn't work here, the calling code only knows what you return. Add the data you want to return to the json, or it would be easier to create a concrete class that has properties for all the data you want to return, create an instance of the class, populate it and serialise it to json to return.

1 solution

The viewbag is re-initialized with each request. You have to find a different way to persist data between requests. There are several ways you can do this, and which method you use really depends on your requirements. Google is your friend.

mvc persist dat between requests - Google Search[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900