Wednesday, March 24, 2010 10:11 PM Kazi Manzur Rashid

Creating Rich View Components in ASP.NET MVC

One of the nice thing of our Telerik Extensions for ASP.NET MVC is, it gives you an excellent extensible platform to create rich view components. In this post, I will show you a tiny but very powerful ListView Component. Those who are familiar with the Webforms ListView component already knows that it has the support to define different parts of the component, we will have the same kind of support in our view component. Before showing you the markup, let me show you the screenshots first, lets say you want to show the customers of Northwind database as pagable list of business card (Yes the example is inspired from our RadControls Suite)

ListView-Customers

And here is the markup of the above view component.

<h2>Customers</h2>
<% Html.Telerik()
       .ListView(Model)
       .Name("customers")
       .PrefixUrlParameters(false)
       .BeginLayout(pager =>
                     {%>
                        <table border="0" cellpadding="3" cellspacing="1">
                            <tfoot>
                                <tr>
                                    <td colspan="3" class="t-footer">
                                        <% pager.Render(); %>
                                    </td>
                                </tr>
                            </tfoot>
                            <tbody>
                                <tr>
                      <%})
       .BeginGroup(() =>
                     {%>
                        <td>
                      <%})
       .Item(item =>
                     {%>
                        <fieldset style="border:1px solid #e0e0e0">
                            <legend><strong>Company Name</strong>:<%= Html.Encode(item.DataItem.CompanyName) %></legend>
                            <div>
                                <div style="float:left;width:120px">
                                    <img alt="<%= item.DataItem.CustomerID %>" src="<%= Url.Content("~/Content/Images/Customers/" + item.DataItem.CustomerID + ".jpg") %>"/>
                                </div>
                                <div style="float:right">
                                    <ul style="list-style:none none;padding:10px;margin:0">
                                        <li>
                                            <strong>Contact Name:</strong>
                                            <%= Html.Encode(item.DataItem.ContactName) %>
                                        </li>
                                        <li>
                                            <strong>Title:</strong>
                                            <%= Html.Encode(item.DataItem.ContactTitle) %>
                                        </li>
                                        <li>
                                            <strong>City:</strong>
                                            <%= Html.Encode(item.DataItem.City)%>
                                        </li>
                                        <li>
                                            <strong>Country:</strong>
                                            <%= Html.Encode(item.DataItem.Country)%>
                                        </li>
                                        <li>
                                            <strong>Phone:</strong>
                                            <%= Html.Encode(item.DataItem.Phone)%>
                                        </li>
                                        <li>
                                            <div style="float:right">
                                                <%= Html.ActionLink("Edit", "Edit", new { id = item.DataItem.CustomerID }) %>
                                                <%= Html.ActionLink("Delete", "Delete", new { id = item.DataItem.CustomerID })%>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </fieldset>
                     <%})
       .EmptyItem(() =>{%>
                        <fieldset style="border:1px solid #e0e0e0">
                            <legend>Empty</legend>
                        </fieldset>
                     <%})
       .EndGroup(() =>
                     {%>
                        </td>
                      <%})
       .EndLayout(pager =>
                     {%>
                                </tr>
                            </tbody>
                        </table>
                     <%})
       .GroupItemCount(3)
       .PageSize(6)
       .Pager<NumericPager>(pager => pager.ShowFirstLast())
       .Render(); %>

As you can see that you have the complete control on the final angel brackets and like the webform’s version you also can define the templates. You can also use this component to show Master/Detail data, for example the customers and its order like the following:

ListView-Orders

I am attaching the complete source code along with the above examples for your review, what do you think, how about creating some component with our extensions?

Download: MvcListView.zip

Shout it
Filed under: , , , ,

Comments

# Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 11:43 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 11:45 AM by Erik

Very nice.

I have to admit, our company uses TelEriks webform controls, which I'm afraid to say I'm not so fond of, but that's another discussion.

You've really done something awesome with the mvc controls, allowing total control to the developer, yet still saving a lot of time..

I think that THE thing about the Telerik mvc components is actually not the final output/screenshots, but more the change towards clean code and clean semantic html output.

It's changing the state of mind for the developer using your controls, not to drag n' drop then forger, but to be aware of all that's going in the application.

The only thing bothering me in your sample is the inline styling instead of class names, but I'm sure that's just for the quick example..

Cheers, and keep up the good work (and keep it open sourced :D)

Erik

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 1:21 PM by John

Hooray for the Fluent interface.

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 2:24 PM by Kazi Manzur Rashid

@Erik: Thanks for the comment.

Nothing in the world is perfect, but as a company we are trying to reach both the camps, which is important :-)

Yes you are correct on the quick example.

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 2:25 PM by Kazi Manzur Rashid

@John: The Fluent interface is available from the early days of Telerik MVC Components :-).

# Twitter Trackbacks for Creating Rich View Components in ASP.NET MVC - Kazi Manzur Rashid's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Creating Rich View Components in ASP.NET MVC - Kazi Manzur Rashid's Blog         [asp.net]        on Topsy.com

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 5:07 PM by SG

I cannot be the only one who thinks the fluent interface mixed in with the HTML looks absolutely horrific.  Talk about some ugly looking code.

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 5:22 PM by Kazi Manzur Rashid

@SG: How about an alternate?

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 6:05 PM by Matt Hidinger

@SG:

Partial views should be able to clean up the code with no change at all to the component. I haven't tested or seen his source code, but I imagine this would work fine:

Make a strongly-typed partial view to encapsulate your template markup, for example: ListPager.ascx

<%@ Control Inherits="ViewUserControl<Pager>" %>

<table border="0" cellpadding="3" cellspacing="1">

                           <tfoot>

                               <tr>

                                   <td colspan="3" class="t-footer">

                                       <% Model.Render(); %>

                                   </td>

                               </tr>

                           </tfoot>

                           <tbody>

                               <tr>

-- END Partial

Then change the ListView configuration to:

<% Html.Telerik()

      .ListView(Model)

      .Name("customers")

      .PrefixUrlParameters(false)

      .BeginLayout(pager => Html.RenderPartial("ListPager", pager))

      .BeginGroup() ..... ETC

      .Render();

%>

-Matt

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 6:06 PM by Dmitry S.

An alternate solution could be method overloads that a partial view name and the model, if necessary.

# re: Creating Rich View Components in ASP.NET MVC

Wednesday, March 24, 2010 6:45 PM by Kazi Manzur Rashid

@Matt: You can even use Html.DisplayFor(item.DataItem) in the item template, but that does not address the actual issue that was raised by SG, You still have to have some C# code to glue everything, so my point was what is better than the Fluent syntax which would look more elegent than this?

# re: Creating Rich View Components in ASP.NET MVC

Thursday, March 25, 2010 11:29 AM by Doug

Is this tool currently in the MVC Extensions you have? I don't see any other demos for it on the Telerik site. I have recently made use of the Grid view control and it's been fantastic. It saved me from having to write several search, sort and paging functions thanks to the built in capability.

# re: Creating Rich View Components in ASP.NET MVC

Thursday, March 25, 2010 11:39 AM by Kazi Manzur Rashid

@Doug: AspNetMvcExtensibility is my pet project. Telerik does not own it. Nice to hear that you found our Grid helpful

# DotNET News

Thursday, March 25, 2010 2:40 PM by DotNET News

DotNET News

# Creating Rich View Components in ASP.NET MVC

Thursday, March 25, 2010 11:09 PM by iAwaaz-News-by-People

Thank you for submitting this cool story - Trackback from iAwaaz-News-by-People

# Daily tech links for .net and related technologies - Mar 26-28, 2010

Friday, March 26, 2010 4:55 AM by Sanjeev Agarwal

Daily tech links for .net and related technologies - Mar 26-28, 2010 Web Development Creating Rich View

# re: Creating Rich View Components in ASP.NET MVC

Thursday, April 01, 2010 9:41 AM by Tim

Hi,

I am troubling opening the solution, in particular the Telerik.Web.Mvc.Playground.Examples.

The project file cannot be loaded.

I have ASP.NET MVC 2 and the Telerik Controls already installed.

Thanks,

Tim

# re: Creating Rich View Components in ASP.NET MVC

Thursday, April 01, 2010 10:13 AM by Kazi Manzur Rashid

@Tim : Could pls provide some more info, what message it is throwing or may be you can send me the screenshots to rashid@telerik.com

# re: Creating Rich View Components in ASP.NET MVC

Thursday, April 01, 2010 12:36 PM by mike kidder

Kazi, excellent samples as always.   I really like the fluent interface as I see it as a template and the total control you can achieve with it.

fyi, your NextPreviousPager has an extra 'Writer.RenderEndTag()' at line 97 that needs removal.

Great stuff!

# re: Creating Rich View Components in ASP.NET MVC

Thursday, April 01, 2010 1:19 PM by Kazi Manzur Rashid

@Tim: I have uploaded the attachment again which I think solve the issue.

@Mike: Thanks for pointing that out.

# Creating Rich View Components in ASP.NET MVC | 007Nova Articles

Pingback from  Creating Rich View Components in ASP.NET MVC | 007Nova Articles