[Translation] ASP. net mvc Tip #14-auxiliary method for template Creation

Source: Internet
Author: User
Tags ruby on rails
[Translation] ASP. net mvc Tip #14-auxiliary method for template Creation

Address: http://weblogs.asp.net/stephenwalther/archive/2008/07/07/asp-net-mvc-tip-14-create-a-template-helper-method.aspx

Translation: Anders Liu

Abstract: In this Tip, you will learn how to create and use templates when displaying database data in the MVC framework. Stephen Walther describes how to create an auxiliary method named RenderTemplate.

On the weekend of March July 4, when I returned to my home in California, I talked to my smart brother about ASP. NET Web Forms, ASP. net mvc and Ruby on Rails create different Web applications. I lamented that when I created an ASP. net mvc application, I really missed the control. In particular, I miss the clear separation between HTML and UI logic brought by templates in ASP. NET Web Forms controls. The Repeater control is really different from the for... next loop.

My brother told me something surprising. He said, "Ruby on Rails has templates. They call them partials ." I didn't understand it at first. My partials in Ruby on Rails is more or less like the user control in ASP. net mvc. However, my brother explained to me that when a partial is presented in a Ruby on Rails application, you can pass a set of items to it. Each item in the set is presented by partial.

Cool. You can create a template in the ASP. net mvc application in the same way. Create a new helper method that accepts the path of an IEnumerable and a user control. For every element in IEnumerable, the Helper method uses the user control as a template. Listing 1 contains a helper method named RenderTemplate.

Listing 1-TemplateExtensions. cs

<Br/> using System; <br/> using System. text; <br/> using System. collections; <br/> using System. web. mvc; </p> <p> namespace Helpers <br/>{< br/> public static class TemplateExtensions <br/>{< br/> public static string RenderTemplate (this HtmlHelper helper, IEnumerable items, string virtualPath) <br/>{< br/> var sb = new StringBuilder (); <br/> foreach (object item in items) <br/>{< br/> sb. append (helper. renderUserControl (virtualPath, item); <br/>}< br/> return sb. toString (); <br/>}< br/>

Suppose you want to display a movie list. You can use HomeController in Listing 2 to return a collection of movie objects. The Index () operation executes a LINQ to SQL query and passes the query result to the Index view.

Listing 2-HomeController. cs

<Br/> using System; <br/> using System. collections. generic; <br/> using System. linq; <br/> using System. web; <br/> using System. web. mvc; <br/> using Tip14.Models; </p> <p> namespace Tip14.Controllers <br/>{< br/> public class HomeController: controller <br/>{< br/> private MovieDataContext _ dataContext = new MovieDataContext (); </p> <p> public ActionResult Index () <br/>{< br/> var movies = _ dataContext. movies; <br/> return View (movies); <br/>}< br/>

The view in listing 3 simply calls the RenderTemplate () method, and passes the path of ViewData. Model and an MVC user control that contains every movie template to this method.

Listing 3-Index. aspx

<Title> </p> <p>

MovieTemplate. ascx MVC user control is strongly typed. Listing 4 lists the background code of the user control. Note the strong type of the user control so that it can only display the Movie object.

Listing 4-MovieTemplate. ascx. cs

<Br/> using System; <br/> using System. collections. generic; <br/> using System. linq; <br/> using System. web; <br/> using System. web. mvc; <br/> using Tip14.Models; </p> <p> namespace Tip14.Views. home <br/>{< br/> public partial class MovieTemplate: System. web. mvc. viewUserControl <movie> <br/>{< br/>}< br/> </movie>

Finally, listing 5 shows the view of the MVC user control. Note that you can use expressions such as ViewData. Model. Title and ViewData. Model. Director to display the Title and Director of a movie. These expressions work because you use a strong type for the MVC user control so that it can only display movie objects.

Listing 5-MovieTemplate. ascx

<Strong> </strong> <br/> Director: </p> <pr/>

When you request the Index view, you will get the page shown in Figure 1. Note that an MVC user control is displayed for each movie.

Figure 1-use a template to present a movie record

Summary

In this Tip, I introduced how to create and use templates in ASP. NET MVC applications. I demonstrated how to create a template by creating an MVC user control and how to use a template to present a group of database records. In the future, you will no longer have to worry about using the Repeater control in ASP. net mvc applications.

Download the source code here: http://weblogs.asp.net/blogs/stephen enwalther/downloads/tip14/tip14.zip.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.