title: line breaks in textarea used in a MVC C# website app
body: <p>I'm using ASP.net MVC C# in Visual Studio Web Dev. I have a couple of textareas which are populated with data and then updated to a database record. </p>
  
  <p>Is it possible to have line breaks saved when a record is updated to the database? I currently view the data on the homepage, but at the moment if someone writes couple of paragraphs (including line breaks) the formatting will be lost.</p>
  
  <p>If this isn't possible no problem, but just wanted to ask if it is. Thanks.</p>
  
  <p>The code on the View page looks like this:</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
          @Html.TextAreaFor(model =&gt; model.para1, new { cols = 75, @rows = 5 })
          @Html.ValidationMessageFor(model =&gt; model.para1)
  &lt;/div&gt;
  </code></pre>
  
  <p>I then have a button that submits the form.</p>
  
  <p>The Controller code that handles the submission looks like this:</p>
  
  <pre><code>[HttpPost]
      public ActionResult Update(Data data)
      {
          if (ModelState.IsValid)
          {
              data.ID = 1; //EF need to know which row to update in the database.
              db.Entry(data).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index", "Home");
          }
          return View(data);
      }
  </code></pre>
  
  <p>and the Model code for the database looks like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  using System.ComponentModel.DataAnnotations;
  
  namespace DFAccountancy.Models
  {
      public class Data
      {
          [DataType(DataType.MultilineText)]
          public int ID { get; set; }
          public string para1 { get; set; }
          public string para2 { get; set; }
      }
  
      public class DataDBContext : DbContext
      {
          public DbSet&lt;Data&gt; Data { get; set; }
      }
  }
  </code></pre>
  
  <p>===========================================</p>
  
  <h1>the Homepage code</h1>
  
  <pre><code>@model IEnumerable&lt;DFAccountancy.Models.Data&gt;
  
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;
      DF Accountancy
  &lt;/h2&gt;
  &lt;div&gt;
  
  &lt;fieldset&gt;
  &lt;legend&gt;About us&lt;/legend&gt;
  
  @foreach (data in Model)
  {
  
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td rowspan="2" width="50%"&gt;
              &lt;b&gt;
                  Suspendisse lectus massa, feugiat at cursus ac, sollicitudin a metus.     Quisque adipiscing commodo sem vitae eleifend. 
              Maecenas ante risus, hendrerit ac tempor et, feugiat eu sapien. Sed sem massa, sodales a varius sit amet, porta in 
              turpis. Duis ullamcorper magna sed risus lobortis luctus. Quisque volutpat enim ut erat tristique sit amet posuere 
              sem ullamcorper. Nulla consequat lectus in sapien sagittis cursus. Quisque elit augue, luctus sed semper non, fringilla 
              sed quam. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Fusce vitae 
              augue quis nisi tincidunt ullamcorper. Duis posuere ultricies turpis at dictum. Vivamus at odio eros. Nunc orci 
              lectus, ornare non tincidunt sed, venenatis id lorem. Nulla ullamcorper, leo quis pellentesque sollicitudin, dui 
              libero vehicula lectus, lobortis consequat orci dui in augue. Ut gravida enim convallis sem luctus sit amet eleifend 
              lorem malesuada. Suspendisse in augue diam, eu laoreet diam.
              &lt;/b&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;div class="display-field"&gt;
                      @Html.Raw(data.para1.Replace(Environment.NewLine, "&lt;br/&gt;"))
                  &lt;/div&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;    
              &lt;td&gt;
                  &lt;div class="display-field"&gt;
                      @Html.Raw(data.para2.Replace(Environment.NewLine, "&lt;br/&gt;"))
                  &lt;/div&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  &lt;/table&gt;
  }
  
          &lt;/fieldset&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>==========================================</p>
  
  <h1>The full Update View page code</h1>
  
  <pre><code>@model DFAccountancy.Models.Data
  
  @{
      ViewBag.Title = "Update";
      }
  
  
  
  &lt;h2&gt;Update&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;    &lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      $(function () { $("#cl_button1").click(function () { $("#para1").val(""); }); });
      $(function () { $("#cl_button2").click(function () { $("#para2").val(""); }); });
  &lt;/script&gt;
  
  @using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;Data&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.para1)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.TextAreaFor(model =&gt; model.para1, new { cols = 75, @rows = 5 })
          @Html.ValidationMessageFor(model =&gt; model.para1)
          &lt;input id="cl_button1" type="button" value="Clear Paragraph" /&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.para2)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.TextAreaFor(model =&gt; model.para2, new { cols = 75, @rows = 5 })
          @Html.ValidationMessageFor(model =&gt; model.para2)
          &lt;input id="cl_button2" type="button" value="Clear Paragraph" /&gt;
      &lt;/div&gt;
  
  
  
      &lt;p&gt;
          &lt;input type="submit" value="Update" /&gt;
          &lt;input type="reset" value="Re-Set to begining" /&gt;
      &lt;/p&gt;
  
  &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>