ahmet aydin
ahmet aydin

Reputation: 116

why textBox value from ViewBag is corrupted?

I am working on a project with asp.net MVC 5 which I am new. At some point I retrieve a string from byte array and pass it to view with ViewBag

string content = System.Text.Encoding.UTF8.GetString(metadatacontent.content);
ViewBag.content = content;

And I want to edit this field in the view.

@ViewBag.content // this line for checking
@Html.TextBox("metaData", (String)ViewBag.content)

What I see on the webpage is: log file (from @ViewBag.content) l�o�g� �f�i�l�e� (inside the textBox)

Why the string is broken inside the textBox? I would appreciate any help.

Thanks.

Edit: Here I converted the text from View into byte[] and stored it using the model.

byte[] bytes = new byte[Request["metaData"].ToString().Length * sizeof(char)];
System.Buffer.BlockCopy(Request["metaData"].ToString().ToCharArray(), 0, bytes, 0, bytes.Length);
metadatacontent.content = bytes;

Upvotes: 0

Views: 449

Answers (1)

dkozl
dkozl

Reputation: 33364

To sum up comments. Because you're not changing default format of the original string you need to use Unicode encoding instead of UTF-8:

string content = System.Text.Encoding.Unicode.GetString(metadatacontent.content);

Upvotes: 2

Related Questions