ASP. net mvc music store-10. Complete navigation and site design

Source: Internet
Author: User
Tags actionlink

 

We have completed most of the work on the website, but there are some navigation functions, home pages, and shop browsing pages added to the site.

Create a shopping cart summary view

 

We hope to see the quantity in the shopping cart on the page of the whole site.

 

You can easily create a partial view and add it to the website layout,

As we can see above, the ShoppingCart controller contains an Action method named CartSummary and returns the segment view.

//

// GET:/ShoppingCart/CartSummary

[ChildActionOnly]

Public ActionResult CartSummary ()

{

Var cart = ShoppingCart. GetCart (this. HttpContext );

ViewData ["CartCount"] = cart. GetCount ();

Return PartialView ("CartSummary ");

} Right-click the Action method, or right-click the Views/ShoppingCart folder and choose create new view and name it CartSummary. Check the create branch view check box.

 

 

 

The CartSummary branch view is very simple. It only links to the Index of ShoppingCart and displays the quantity in the current Shopping Cart. The complete code is as follows:

@ Html. ActionLink ("Cart (" + ViewData ["CartCount"] + ")", "Index", "ShoppingCart", new {id = "cart-status "})

 

The segment view can be included on any page of the website. You can use the Html. RenderAction method. RenderAction must specify the Action name. Here is CartSummary and the Controller name. Here is ShoppingCart.

@ Html. RenderAction ("CartSummary", "ShoppingCart ")

 

Before adding this branch view to the layout, we need to create a genre menu so that we can update the Site. master at one time.

Create a branch view of the genre menu

 

By adding a genre menu on the page, it is easier for users to navigate within the site.

 

 

We can use the preceding steps to create the branch view of the genre menu and add the two branch views together to the layout of the site. First, add the GenreMenu Controller Method to StoreController.

//

// GET:/Store/GenreMenu

[ChildActionOnly]

Public ActionResult GenreMenu ()

{

Var genres = storeDB. Genres. ToList ();

Return PartialView (genres );

} This method returns the list of genres, which are used to generate menus in the view created later.

 

Note: we added the [ChildActionOnly] annotation in the Action method, which means that we can only access this Action through the Division view, which can prevent access through browsing/Store/GenreMenu, this is not necessary for the branch view, but it is a good practice because we want our Controller method to be used in the desired way, here we also return a partial view instead of a common view, which is used to tell the view engine that layout is not required for this view and will be included in other views.

Create a division view and use a strongly typed Genre as the model type. Use the List template.

 

Update the generated view to display a list.

 

 

@ Model IEnumerable <MvcMusicStore. Models. Genre>

<Ul id = "categories">

@ Foreach (var genre in Model)

{<Li> @ Html. ActionLink (genre. Name,

"Browse", "Store ",

New {Genre = genre. Name}, null)

</Li>

}

</Ul>

 

Update the site layout to display our branch view

 

Now, you can add the branch view to the Layout, in/Views/Shared/_ Layout. in cshtml, Html is called. the RenaderAction () method can call the Division view and add both the Division views to the layout, as shown below:

 

<! DOCTYPE html>

<Html>

<Head>

<Title> @ ViewBag. Title </title>

<Link href = "@ Url. Content ("~ /Content/Site.css ")" rel = "stylesheet"

Type = "text/css"/>

<Script src = "@ Url. Content ("~ /Scripts/jquery-1.4.4.min.js ")"

Type = "text/javascript"> </script>

</Head>

<Body>

<Div id = "header">

<H1>

<A href = "/"> ASP. net mvc music store </a>

<Ul id = "navlist">

<Li class = "first"> <a href = "@ Url. Content ("~ ")" Id = "current"> Home </a> </li>

<Li> <a href = "@ Url. Content ("~ /Store/")"> Store </a> </li>

<Li>

@ {Html. RenderAction ("CartSummary", "ShoppingCart ");}

</Li>

<Li> <a href = "@ Url. Content ("~ /StoreManager/")"> Admin </a> </li>

</Ul>

</Div>

@ {Html. RenderAction ("GenreMenu", "Store ");}

<Div id = "main">

@ RenderBody ()

</Div>

<Div id = "footer">

Built with <a href = "http://asp.net/mvc"> ASP. net mvc 3 </a>

</Div>

</Body>

</Html>

 

Update the Store Browse page

 

The browsing page of the store is not very good now. We update this page to display the album in a better layout, and update our view as follows.

@ Model MvcMusicStore. Models. Genre

@ {ViewBag. Title = "Browse Albums ";}

<Div class = "genre">

<H3>

<Em> @ Model. Name </em> Albums

 

<Ul id = "album-list">

@ Foreach (var album in Model. Albums)

{<Li> <a href = "@ Url. Action (" Details ", new {id = album. AlbumId})">

<Span> @ album. Title </span> </a> </li>}

</Ul>

</Div>

 

 

Here, we will use Url. Action instead of Html. ActionLink to display formatting information, including the artist's illustration.

Note: We will display the album cover. This information is stored in the data and can be edited through StoreManager. You are also welcome to add your illustrations.

Now, when we browse the genre, we will see an album with a cover shown in a grid.

 

 

 

Update the homepage to display best-selling albums

 

We hope to add the best-selling albums on the home page to improve sales. We will add some content in HomeController to achieve this, and then add some additional images to make it better.

First, add a navigation attribute to our album to let EF know the associated information. The last line in the album is newly added.

Public virtual Genre {get; set ;}

Public virtual Artist {get; set ;}

Public virtual List <OrderDetail> OrderDetails {get; set ;}

 

Note: a Generic set is used here. You need to use using before the code to reference the System. Collections. Generic namespace.

First, we will add the storeDB field and reference the MusicStore. Models namespace, similar to other controllers.

Then, we add the following method in HomeController to query the database and find the best-selling records based on OrderDetails.

Private List <Album> GetTopSellingAlbums (int count)

{

// Group the order details by album and return

// The albums with the highest count

Return storeDB. Albums

. OrderByDescending (a => a. OrderDetails. Count ())

. Take (count)

. ToList ();

}

 

This is a private method, because we do not want to directly access it. In order to simply write it in HomeController, we may need to move it to the background logic service during actual development.

Here, we update the Index to access the previously defined method, query the top five sales albums, and pass them to the view.

Public ActionResult Index ()

{

// Get most popular albums

Var albums = GetTopSellingAlbums (5 );

Return View (albums );

}

 

The complete code is as follows:

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Web;

Using System. Web. Mvc;

 

Using MvcMusicStore. Models;

 

Namespace MvcMusicStore. Controllers

{

Public class HomeController: Controller

{

Private Models. MusicStoreEntities storeDB = new Models. MusicStoreEntities ();

//

// GET:/Home/

 

Public ActionResult Index ()

{

// Get most popular albums

Var albums = GetTopSellingAlbums (5 );

Return View (albums );

}

 

Private List <Album> GetTopSellingAlbums (int count)

{

// Group the order details by album and return

// The albums with the highest count

Return storeDB. Albums

. OrderByDescending (a => a. OrderDetails. Count ())

. Take (count)

. ToList ();

}

}

}

 

Finally, we need to update the Index view of our Home controller and add the access model to the album list. At this time, we need to add a header and a promotion section.

@ Model List <MvcMusicStore. Models. Album>

@{

ViewBag. Title = "ASP. net mvc Music Store ";

}

<Div id = "promotion">

</Div>

<H3>

<Em> Fresh </em> off the grill

<Ul id = "album-list">

@ Foreach (var album in Model)

{<Li> <a href = "@ Url. Action (" Details "," Store ",

New {id = album. AlbumId}) ">

<Span> @ album. Title </span> </a> </li>

}

</Ul>

 

Now, when running the program, we will see the updated home page with the best-selling albums and our promotion information.

 

 

 

Author champion

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.