Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Write Less Do More with ASP.NET MVC Templates

0.00/5 (No votes)
3 Jun 2014 1  
Write less do more with ASP.NET MVC templates

ASP.NET MVC 5 has a host of awesome features that can be leveraged to make a good and maintainable website. One of these features is the templating system.
MVC offers two types of templates for views, namely:

  • Display Templates: Used for displaying data
  • Editor templates: Used for creating an editor for data

<img alt="Ninja" class="wlEmoticon wlEmoticon-ninja" src="https://ankeshdave.files.wordpress.com/2014/05/wlemoticon-ninja1.png?w=720" style="border-style:none;" />WPF and Silverlight developers users will find a lot of similarity in the MVC templating system.<img alt="Ninja" class="wlEmoticon wlEmoticon-ninja" src="https://ankeshdave.files.wordpress.com/2014/05/wlemoticon-ninja1.png?w=720" style="border-style:none;" />

The MVC templating system uses DataType as an identifier to select the appropriate template for use. In order to use the templates, we have to use the following syntax so that MVC can invoke the template selection.

For Display templates:

@Html.DisplayFor(m=> m.Myproperty)

where m can be the model for the view.

For Editor Templates:

@Html.EditorFor(m=> m.Myproperty)

where m can be the model for the view.

For demo, we will create/modify the editor template for data type String. In this demo, I have purposefully added some CSS to make the Display and Editor look different.

Display templates and Editor templates are saved in specific folders inside the Views\\Shared\\<folders>.

<img alt="image_thumb" src="781787/image_thumb_thumb1.png" style="display: block; border: 0px; width: 281px; height: 145px; background-image: none;" title="image_thumb" />

From the above figure, you can understand that we have created a display as well as an editor template for the data type String.

For Display Template, the file “String.cshtml” has:

@model String

<!--Using some bootstrap CSS classes to make it look distinct-->

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Display Template</h3>
    </div>
    <div class="panel-body">
        @ViewData.TemplateInfo.FormattedModelValue
    </div>
</div>

For Editor Template, the file “String.cshtml” has:

@model String

<!--Using some bootstrap CSS classes to make it look distinct-->

<div class="panel panel-success">
    <div class="panel-heading">
        <h3 class="panel-title">Editor Template</h3>
    </div>
    <div class="panel-body">
        @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)
    </div>
</div>

The job is almost done, we just have to use these templates in one of the views, I will create a separate View/Controller (TemplateDemoController) to keep this tutorial simple.

I have created a “Person” model with a string property “Name” and passed it as the model for my view.

public class TemplateDemoController : Controller
{
    // GET: TemplateDemo
    public ActionResult Index()
    {
        return View(new Person() {Name = "Some Name"});
    }
}

And I will use the Displayfor and EditorFor helpers in my view to invoke template selection while view rendering.

@model BootswatchDemo.Models.Person

@{
    ViewBag.Title = "Template Demo";
}

<h2>@ViewBag.Title</h2>

<div>@Html.DisplayFor(m => m.Name)</div>

<div>@Html.EditorFor(m => m.Name)</div>

After this, compile and run the website and navigate to the respective view to see the result.

<img alt="image" src="781787/image_thumb2.png" style="display: block; border-width: 0px; width: 640px; height: 215px; background-image: none;" title="image" />

<img alt="Ninja" class="wlEmoticon wlEmoticon-ninja" src="https://ankeshdave.files.wordpress.com/2014/05/wlemoticon-ninja1.png?w=720" style="border-style:none;" /> Using this feature, people will save a lot of time by creating templates for custom classes instead of writing HTML in every View.<img alt="Ninja" class="wlEmoticon wlEmoticon-ninja" src="https://ankeshdave.files.wordpress.com/2014/05/wlemoticon-ninja1.png?w=720" style="border-style:none;" />

I hope this post might have helped you to understand the concept of templates in MVC and will help you in your projects/assignments. If you like the article, please share or comment. Your suggestions are welcome.

Bye for now, I will post more articles soon with some more interesting stuff as my journey of exploring ASP.NET MVC continues.

Source code: https://github.com/ankeshdave/Blogs/tree/master/MVC%20Templates%20Demo

Filed under: Tutorial

Tagged: ASP.NET MVC, Bootstrap, C#, Razor, Templates

<img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ankeshdave.wordpress.com/116/" /> <img alt="" border="0" height="1" src="http://stats.wordpress.com/b.gif?host=ankeshdave.wordpress.com&blog=51840434&post=116&subd=ankeshdave&ref=&feed=1" width="1" />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here