26.06.2017 Views

Bootstrap for ASP.NET MVC

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2<br />

To generate multiline textboxes that look similar across our site and <strong>for</strong> example,<br />

always renders three rows, per<strong>for</strong>m the following steps:<br />

1. Add a new ile called MultilineText.cshtml to the EditorTemplates<br />

folder.<br />

2. Add the following code to the ile:<br />

@model string<br />

@Html.TextArea("", ViewData.TemplateInfo.FormattedModelValue.<br />

ToString(),new { @class = "<strong>for</strong>m-control", rows = 3 })<br />

This code renders a text area element using the TextArea HTML helper and<br />

sets its class property to use the <strong>Bootstrap</strong> <strong>for</strong>m-control style as well as<br />

specifying the number of rows the multiline textbox should render.<br />

In order <strong>for</strong> the view to use the correct editor template, we need to make an<br />

adjustment to our model. By adding a DataType attribute to the multiline<br />

property in our model, we can specify which editor template should be used<br />

when rendering the speciic property. For example, in the following code,<br />

we'll specify that the Description property should use the MultiLineText<br />

editor template:<br />

[DataType(DataType.MultilineText)]<br />

public string Description { get; set; }<br />

[ 49 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!