arth81
arth81

Reputation: 229

How to add file content to Viewbag mvc4

Is it possible to add content of txt file to Viewbag being inside the Controller? How to do it?

Upvotes: 2

Views: 703

Answers (2)

chiapa
chiapa

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

Vladimirs
Vladimirs

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

Related Questions