Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

line breaks in textarea used in a MVC C# website app

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.

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.

If this isn't possible no problem, but just wanted to ask if it is. Thanks.

The code on the View page looks like this:

<div class="editor-field">         @Html.TextAreaFor(model => model.para1, new { cols = 75, @rows = 5 })         @Html.ValidationMessageFor(model => model.para1) </div> 

I then have a button that submits the form.

The Controller code that handles the submission looks like this:

[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);     } 

and the Model code for the database looks like this:

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<Data> Data { get; set; }     } } 

===========================================

the Homepage code

@model IEnumerable<DFAccountancy.Models.Data>  @{     ViewBag.Title = "Index"; }  <h2>     DF Accountancy </h2> <div>  <fieldset> <legend>About us</legend>  @foreach (data in Model) {  <table>     <tr>         <td rowspan="2" width="50%">             <b>                 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.             </b>             </td>             <td>                 <div class="display-field">                     @Html.Raw(data.para1.Replace(Environment.NewLine, "<br/>"))                 </div>             </td>         </tr>         <tr>                 <td>                 <div class="display-field">                     @Html.Raw(data.para2.Replace(Environment.NewLine, "<br/>"))                 </div>             </td>         </tr> </table> }          </fieldset> </div> 

==========================================

The full Update View page code

@model DFAccountancy.Models.Data  @{     ViewBag.Title = "Update";     }    <h2>Update</h2>  <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">    </script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>  <script type="text/javascript">     $(function () { $("#cl_button1").click(function () { $("#para1").val(""); }); });     $(function () { $("#cl_button2").click(function () { $("#para2").val(""); }); }); </script>  @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset>     <legend>Data</legend>      <div class="editor-label">         @Html.LabelFor(model => model.para1)     </div>     <div class="editor-field">         @Html.TextAreaFor(model => model.para1, new { cols = 75, @rows = 5 })         @Html.ValidationMessageFor(model => model.para1)         <input id="cl_button1" type="button" value="Clear Paragraph" />     </div>      <div class="editor-label">         @Html.LabelFor(model => model.para2)     </div>     <div class="editor-field">         @Html.TextAreaFor(model => model.para2, new { cols = 75, @rows = 5 })         @Html.ValidationMessageFor(model => model.para2)         <input id="cl_button2" type="button" value="Clear Paragraph" />     </div>        <p>         <input type="submit" value="Update" />         <input type="reset" value="Re-Set to begining" />     </p>  </fieldset> }  <div>     @Html.ActionLink("Back to List", "Index") </div> 
like image 720
Harry Avatar asked Mar 23 '12 17:03

Harry


1 Answers

When displaying the field as html, it will not include line breaks since they are treated as spaces in html markup. You could replace them with <br/>. It would look something like this:

<div class="display-field">    @Html.Raw(Model.para1.Replace(Environment.NewLine, "<br/>")) </div> 

Or you could display it as preformatted text, which will preserve white space:

<div class="display-field">     <pre>         @Model.para1     </pre> </div> 

Update If your model is IEnumerable<T>:

@foreach (var data in Model) {     <div class="display-field">        @Html.Raw(data.para1.Replace(Environment.NewLine, "<br/>"))     </div> } 

or

@foreach (var data in Model) {     <div class="display-field">         <pre>             @data.para1         </pre>     </div> } 
like image 198
jrummell Avatar answered Sep 20 '22 15:09

jrummell