[Mvc] Using Repeater in ASP. NET MVC

Source: Internet
Author: User

I personally like the MVC development mode, which is very convenient and has a clear structure.
But under MVC, View and code are completely separated, and there is no FORM on the server side, which means that almost all server controls cannot be used! (Insider: In fact, I basically never use Server Forms, even in MVP Mode)
In the View of MVC, we basically only use a loop like for foreach to output data, and the content is small, but if there are many, it will be very troublesome.

Although this is flexible, it is a headache to see a section <%> ~~ Is there any way to use the server control? Do not use Form on the server ).

The first thing that comes to mind is Repeater. Repeater is a very useful server control that is easy to use. I like it very much. In addition, the Repeater has a feature that does not depend on the Form on the server ).
So, I wonder if we can use Repeater in Mvc ~~~ The answer is yes. But it seems to be very troublesome to use.
There are several troubles:
1. Repeater needs to manually bind data. This means that we need to write the server code in the View, first retrieve the data from the ViewData, and then bind it to the Repeater.
2. If a page uses n + 1 Repeater, will it go crazy? In addition, to specify an Id for each Repeater, data must be bound. This is a headache !!!

 

So ~~ Can we make Repeater easy to use? Let's Repeater the Repeater to achieve our goal.

We all know that in Mvc, we use ViewData to transmit data. Can we directly bind the Repeater to the data in ViewData? Of course.

Check the source code of the transformed Repeater:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI. WebControls;
Using System. Web. Mvc;

Namespace RepeaterInMvc. Codes
{
Public class MvcRepeater: Repeater
{
/// <Summary>
/// Key name in ViewData
/// </Summary>
Public string Key {get; set ;}

/// <Summary>
/// Obtain the ViewPage object
/// </Summary>
Protected ViewPage
{
Get {return base. Page as ViewPage ;}
}

/// <Summary>
/// Rewrite the Onload event to bind data
/// </Summary>
/// <Param name = "e"> </param>
Protected override void OnLoad (EventArgs e)
{
This. DataSource = this. ViewPage. ViewData [this. Key]; // obtain the data source.
This. DataBind (); // you do not need to manually write N bindings.

Base. OnLoad (e );
}
}
}

 

What did the above Code do:

1. added a public property Key to indicate which data in ViewData is to be bound to the Repeater.
2. added a protection attribute ViewPage, pointing to the current Page object, and forced conversion to the Mvc ViewPage object (because we want to get ViewData or other Mvc-related context information)
3. Rewrite the OnLoad event and rewrite the event to bind data, so that we do not need to manually bind each Repeater in the View. This is so annoying.

 

Now we have achieved our goal. Let's see how to use it:

Controller code:

Public ActionResult Index ()
{
// Test Data
List <Models. TestInfo> entities = new List <RepeaterInMvc. Models. TestInfo> ();

Entities. Add (new RepeaterInMvc. Models. TestInfo {Id = 1, Name = "Kagilo1", Email = "Kagilo@126.com "});
Entities. Add (new RepeaterInMvc. Models. TestInfo {Id = 2, Name = "Kagilo2", Email = "Kagilo@126.com "});
Entities. Add (new RepeaterInMvc. Models. TestInfo {Id = 3, Name = "Kagilo3", Email = "Kagilo@126.com "});
Entities. Add (new RepeaterInMvc. Models. TestInfo {Id = 4, Name = "Kagilo4", Email = "Kagilo@126.com "});
Entities. Add (new RepeaterInMvc. Models. TestInfo {Id = 5, Name = "Kagilo5", Email = "Kagilo@126.com "});

ViewData ["TestList"] = entities;
Return View ();
}

 

Let's look at the page code:

<% @ Register Assembly = "RepeaterInMvc" Namespace = "RepeaterInMvc. Codes" TagPrefix = "mvc" %>

<Asp: Content ID = "indexTitle" ContentPlaceHolderID = "TitleContent" runat = "server">
Home Page
</Asp: Content>

<Asp: Content ID = "indexContent" ContentPlaceHolderID = "MainContent" runat = "server">
<H2> Repeater example <P>
<Mvc: MvcRepeater Key = "TestList" runat = "server">
<ItemTemplate>
<Div style = "height: 30px; line-height: 30px;"> <% # Eval ("Id") %>, <% # Eval ("Name ") %>, <% # Eval ("Email") %> </div>
</ItemTemplate>
<AlternatingItemTemplate>
<Div style = "height: 30px; line-height: 30px; background: # eeeeeeee;"> <% # Eval ("Id") %>, <% # Eval ("Name") %>, <% # Eval ("Email") %> </div>
</AlternatingItemTemplate>
</Mvc: MvcRepeater>
</P>
</Asp: Content>

 
Note: <% @ Register Assembly = "RepeaterInMvc" Namespace = "RepeaterInMvc. Codes" TagPrefix = "mvc" %>
Register the control on the current page !!! Of course, you can also register all pages on the page/controls node in web. config.

See the results:

How is it? Is it nice?

 

Download the source code of this article: RepeaterInMvc.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.