Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a textarea in my mvc application when the user save it for example
hi
how are
you

but when user want to edit that textarea it shows hi how are you

but i want in that way
hi
how are
you

What I have tried:

@Html.TextAreaFor(model => model.Notes, new { @class = "form-control", placeholder = "Name", required = "required", @rows = 5 })
Posted
Updated 29-Sep-16 4:40am
Comments
F-ES Sitecore 29-Sep-16 5:40am    
The line breaks are getting stripped somewhere. Check they are present in the data store you put the text in, then check they're in the data you're showing in your view. It might also be an encoding issue...but ultimately we can't access your system so it's impossible to tell.

1 solution

This solution worked for me as I had similar issue.

In your controller, when saving the note you would do something like

C#
entity.Note = HttpUtility.HtmlEncode(model.Note.Replace(Environment.NewLine, "<br>"));


Then when displaying in your view you could do something like

HTML
<textarea name="Note"><br mode="hold">@(new HtmlString(HttpUtility.HtmlDecode((note.Note1))))<br mode="hold"></textarea>


That should work, I did not compile this/pull this from existing code so you may have to tweak it to meet your needs. But it should hopefully steer you in the right direction.
 
Share this answer
 
Comments
Rakib Ahmed 29-Sep-16 23:52pm    
entity.Note = HttpUtility.HtmlEncode(model.Note.Replace(Environment.NewLine, "
"));
i did debug on this line but i see it is not saving like hi
how
are

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