Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellooo everyone!

I'm still new to MVC, so I'm trying to figure out the different options that I can't currently find that were originally in WinForms.

All I need to do is append text into a ViewBag, rather than just overwriting the previous value.

Scenario:

- Web Request calls API to retrieve data which is a JSON Object that stores a JSON Array.

- Code (Listed Below) loops through and puts text into ViewBag.

- Webpage loads, but has put the very last array value into ViewBag (Which I fully understand is because I'm overwriting the previous value in the ViewBag).

All I want to do is to add the additional Array values into the ViewBag, so that if the array has 1,2,3,4 objects, the ViewBag will display 1,2,3,4 rather than just 4.

In WinForms I would do something simple like
C#
Textbox1.Text +=alertName.toString();
or in a .NET App I would just simply use
C#
textBox1.AppendText("Some text");


Is there a similar way to do this in MVC?

What I have tried:

C#
var jsonClient = new WebClient();
            jsonClient.Credentials = new NetworkCredential("XXXX", "XXXX");
            jsonClient.Headers.Set("Content-Type", "application/json");
            jsonClient.Headers.Set("Accept", "application/json");
            var alertsSettings = jsonClient.DownloadString("http://XXXX:XXXX/api/alerts/settings");
            JObject results = JObject.Parse(alertsSettings);

            foreach (var result in results["data"])
            {
                string alertName = (string)result["alertName"];
                ViewBag.alertNameData = alertName.ToString();
            }
Posted
Comments
F-ES Sitecore 18-Jul-18 6:29am    
If you keep thinking in terms of winforms you're going to struggle doing a web app. Windows apps are stateful...everything you do is preserved. Web apps are stateless...everything you do is immediately thrown away. The reason you can't append to your Viewbag is because your viewbag and its contents are destroyed as soon as your page has been sent to the client browser.

So you need to send the contents of what the viewbag was to your method so that you can append to it and re-return it. The simplest way of doing this is to store the contents of the viewbag in a hidden input element, then submit the content of that element with your next request. How you do that depends on how you are calling your web method which you haven't explained. If this is a normal form then putting the hidden input in the form should be enough, if it's ajax you'll need to pass the contents of the hidden input to the ajax data param.
JoshWigley 18-Jul-18 6:47am    
Thanks for the info! Again, this is all relatively new to me, so apologies if the question appears (most likely) as totally novice.

Your comment has pushed me in the right direction, and I've now assigned the data to a variable outside of the loop, which has achieved exactly what I'm after. I was possibly trying to overcomplicate things, when actually it was incredibly simple.

If you put your comment as a solution, I'll happily accept this as the correct answer.

Appreciate your help buddy! :)

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