Reputation: 229
Is it possible to add content of txt file to Viewbag being inside the Controller? How to do it?
Upvotes: 2
Views: 703
Reputation: 4412
You can achieve that by reading the file into a string and passing it into a ViewBag, for example.
string fileContent = System.IO.File.ReadAllText("C:/path/file.txt");
ViewBag.Whatever = fileContent;
Upvotes: 1
Reputation: 8599
Yes it is possible. You just need to read file contents to a string and than pass that value to Viewbag.
Something like that could work:
ViewBag.FileText = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
Upvotes: 2