Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: Creating a List to View Razor function

$
0
0
We went open source in September 2010 - spread the word in case you should choose to stick with Composite C1 ;-)

Below is a more full blown sample - the following has changed:
  • I renamed variables - so 'artist' is used all over
  • I added URL encoding to artist names (the %20 thing you asked for)
  • Made the code a bit more 'robust' (like 'artisk not found' message on bad urls)
  • I added <html> <head> <body> around stuff - having <head> allow me to write to it on the final page and add css etc. (here I just do inline css - you can include files) - try viewing the source of the final page and you the <style /> tage used here in the final page head.
Please note: If your new URLs give you a "HTTP Error 404.11 - Not Found" error, read this answer on how to fix it. This is Microsoft IIS security trying to overprotect you.
@using Composite.Data;
@using Composite.Core.Routing.Pages;

@using CompositeC1Contrib.RazorFunctions;
@using CompositeC1Contrib.RazorFunctions.Html;
@using Composite.Visual.Artist;

@inherits CompositeC1WebPage

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <!-- stuff we put in the head here will end up in the final page's head section - include css and script files here -->
        <style type="text/css">
            ul li{
                clear:both;
            }
            ul.artistlist img {
                float:left;
                margin-right: 5px;
            }
        </style>
    </head>
    <body>

@{
    // Tell C1 that any extra path info is okay - avoid 404
    C1PageRoute.RegisterPathInfoUsage();

    // get the ID (artist name) from the path info if provided
    string artistPathReq = PathInfo(0);

    // no ID is provided: list data          
    if (string.IsNullOrEmpty(artistPathReq))
    {
            <ul class="artistlist">
                @foreach  (var artist in Data.Get<Composite.Visual.Artist.Names>().OrderBy(p => p.ArtistName))
                {
                    var artistPath = Server.UrlEncode(artist.ArtistName);
                    <li>
                        <a href="/page(@CurrentPageNode.Id)/@artistPath">
                            @if (!string.IsNullOrEmpty(artist.Image))
                            {
                                <img src="@Html.C1().MediaUrl(artist.Image)?mw=150&amp;mh=50" />
                            }
                            @artist.ArtistName
                        </a>
                    </li>
                }
            </ul>        
    }
    // we got an ID - showing detail view:   
    else
    {
        string artistName = Server.UrlDecode(artistPathReq);
        
        var artist = Data.Get<Composite.Visual.Artist.Names>().Where(p => p.ArtistName == artistName).FirstOrDefault();
        
        if (artist!=null)
        {
            <div id="PageDetails">
                <b>Title:</b> @artist.ArtistName <br />
                <b>Bio:</b> @Html.C1().Body(artist.ArtistBio)<br />
                @if (!string.IsNullOrEmpty(artist.Image))
                {
                    <b>Image (full):</b> <img src="@Html.C1().MediaUrl(artist.Image)" /><br />
                }
            </div>
        }
        else
        {
            <p>NOT FOUND: @artistName</p>
        }
        
        <a href="/page(@CurrentPageNode.Id)">Back to List</a><br />
    }  
}

            </body>
</html>


@functions {
    string PathInfo(int segment)
    {
        string  pathInfo = C1PageRoute.GetPathInfo()??string.Empty;
        string [] segments = pathInfo.Split('/');
        return segments.Skip(segment + 1).FirstOrDefault();
    }
}
Enjoy and do keep using the forum :)

Viewing all articles
Browse latest Browse all 2540

Trending Articles