0

I have a textarea where the user can type in input but if they create a newline in the text area I want it to be saved into to the database so it will appear with the newline. How do I do that?

It seems when the string is saved to the data base the /n is removed from the string.

Im using C# with EF6

gfdh
hgfd
hgfd
hgfd
;

This above is what shows in html but the physical display of it is just one line without the newlines

I have tried the br solution but its within the quotes and the br tag is useless if its within the tags. how do I get teh br tag to escape

        public void SendMessageToUser(Message message)
    {
        var db = new database();
        var list = db.Conversations.FirstOrDefault(c => c.UserAId == message.SenderId && c.UserBId == message.ReceiverId || c.UserAId == message.ReceiverId && c.UserBId == message.SenderId);
        if (list != null)
        {
            message.ConversationId = list.ConversationId;
            message.DateSent = DateTime.Now;
            db.Messages.Add(message);
        }
        else
        {
            var conversation = new Conversation();
            conversation.DateStarted = DateTime.Now;
            conversation.ConversationStatusId = 1;
            conversation.UserAId = message.ReceiverId;
            conversation.UserBId = message.SenderId;
            message.ConversationId = db.Conversations.Add(conversation).ConversationId;
            message.MessageStatusId = 1;
            message.DateSent = DateTime.Now;
            db.Messages.Add(message);
        }
        db.SaveChanges();
    }



    public class Message
{
    public int MessageId { get; set; }
    public string Content { get; set; }
    public DateTime DateSent { get; set; }

    [ForeignKey("Conversation")]
    public int ConversationId { get; set; }
    public virtual Conversation Conversation { get; set; }

    public int MessageStatusId { get; set; }
    public virtual MessageStatus MessageStatus { get; set; }

    [ForeignKey("Sender")]
    public int SenderId { get; set; }
    public virtual User Sender { get; set; }

    [ForeignKey("Receiver")]
    public int ReceiverId { get; set; }
    public virtual User Receiver { get; set; }


}

html

 @foreach (var m in Model.Messages)
            {
    <h3>@m.Sender.Username</h3>
                    <p>@m.DateSent.ToShortTimeString()</p>
                    @string.Format("{0}",m.Content);
                    @m.Content;
imGreg
  • 900
  • 9
  • 24
  • You want that when the newline is added data is saved and this newline appears ? Where ? In another textbox ? If yes, get the lastinsertedid and read this new inserted line from database and write it yourself in destinated textbox by adding newline symbole – Ismail Gunes Feb 02 '14 at 21:59
  • I want all the newlines from that text area to appear on a html page within a div. The problem is when it is being saved to the database using EF the "\n" are removed and the text appears only with whitespaces in between the words – imGreg Feb 02 '14 at 22:06
  • I had have the same problem. I resolved by replacing \n by a symbole that user can't use. And saving my data with this symbole and when reading changing it with newline. I hope someone will give you more efficient answer. – Ismail Gunes Feb 02 '14 at 23:21

1 Answers1

0

The text from the text area will be stored with newlines in the database. However if you want to display the text in HTML, you have to replace the newlines to <br /> tags and tell razor to not encode the HTML. You can do that by wrapping your text in a MvcHtmlString.

Here is an example of how you can achive that with a little HtmlHelper. Another way is to display your text using the @Html.Raw("Your text") HtmlHelper.

Also another posibility to display the line breaks is to use white-space: pre-line; so you don't need to allow HTML tags in your code which would make it more secure. (See here)

Community
  • 1
  • 1
mboldt
  • 1,780
  • 11
  • 15
  • When I do this, it just adds
    to the string on the html page. In the html it has " " around them so the break line tags arent going to do what html intended it to do but literally type it out
    – imGreg Feb 02 '14 at 21:32
  • I have tried the br solution but its within the quotes and the br tag is useless if its within the tags. how do I get teh br tag to escape – imGreg Feb 03 '14 at 04:20
  • Can you post some more code how you are doing things? – mboldt Feb 03 '14 at 07:35
  • @imGreg I updated my answer and added some links concerning your problem. – mboldt Feb 04 '14 at 06:46