In this recipe, we are going to look at a method for creating a master/detail page scenario. We will use a partial view to provide us with the formatting of the details. Then we will use a bit of jQuery to do the fetching of that partial view, as well as placing it into the page for us.
This recipe will follow the other recipes in this chapter, in that it will be using the Product
and ProductRepository
classes to provide us with some data to work with, using NBuilder to generate some data for us. You can grab the Product
and ProductRepository
class as well as NBuilder from the previous recipes in this chapter.
Product
and ProductRepository
classes to the Models
folder in our project (which you can get from the previous recipe). Also, drag over the previous, home controller, which has the action code that we need for this recipe to get and show products. Views/Shared
folder. Then expand the Scripts
folder and drag the jquery-1.4.2.js
file (the version that is shipped with Visual Studio) into the header declaration of the master page. Or you can reference the latest version of jQuery by adding this line:Views/Shared/Site.Master:
<script src= http://code.jquery.com/jquery-1.4.2.min.js type="text/javascript"></script>
Products
display page. To do this, create a new view in the Views/Home
folder called Products.aspx
. Open it up and add a quick reference to Products
in the ViewData
dictionary (we will put the products collection in there shortly). Then add a foreach
loop to iterate through the products to display them. We will display the product's price and name. Notice that I pass the ID of the product into the ID used to communicate with our controller, as well as in the ID
attribute of our link.Views/Home/Products.aspx:
<h2>Products</h2> <p> <% List<Product> products = ViewData["Products"] as List<Product>; foreach (Product product in products) { %> <div class="display-field"> <%: String.Format("{0:c}", product.Price)%> <%= Html.ActionLink(product.Name, "ProductDetail", new {@id=product.ProductId}, new {@id=product.ProductId, @class="list-item"}) %></div> <% } %> </p>
Products
display page, we need to add a ProductDetail
page. This page will be a standard page that our users can see in case they don't have JavaScript enabled or, more importantly, in case they are viewing your site from a device that doesn't support JavaScript. To do this, we will create a new page called ProductDetail.aspx
. This page will load a partial view called GetProductDetail
, which will house our product details formatting.Views/Home/ProductDetail.aspx:
<h2>Product Detail</h2> <% Html.RenderPartial("GetProductDetail"); %>
GetProductDetail
partial view (also in the Views/Home
folder called GetProductDetail.ascx)
. This view will expect that a product exists in the ViewData
dictionary, as that is where it will load its details from.Views/Home/GetProductDetail.ascx:
<% Product p = ViewData["Product"] as Product; %> <div class="display-label">Id: </div> <div class="display-field"><%: p.ProductId %></div> ... <div class="display-label">Desc: </div> <div class="display-field"><%: p.Description %></div> <hr />
div
tag to display our product details in. Add this just under the page header before the products display.Views/Home/Products.aspx:
<h2>Products</h2>
<div id="detail"></div>
<p>
head
section of our products page. Then we will add a snippet of jQuery that will select all of the links that have the class list item assigned to them. We will then have jQuery clear the HTML of the div
tag with the ID of detail
(which we just added).Views/Home/Products.aspx:
<script> $(function () { $('a.list-item').click(function () { $("#detail").html(''), }); }); </script>
GetProductDetail
partial view into the HTML of the detail
div. Then we will cancel out the links
event by returning false. Here is the full script:Views/Home/Products.aspx:
<script> $(function () { $('a.list-item').click(function () { $("#detail").html(''), $('#detail').load('<%: Url.Action("GetProductDetail") %>', { id: this.id }); return false; }); }); </script>
This recipe is probably the easiest way to control the formatting of snippets of HTML that you want to inject into your page to create an Ajax site for your users. You simply add a link to your page that sends the user to a details view page. You then take all the display code that you would normally put into your details page and put it into a partial view instead. The partial view is then loaded into the details page, but because it just renders raw HTML, it can also be loaded directly by JavaScript. This makes your site much more flexible with regards to serving clients!
While this is the easiest way to get Ajax into your application, it is by no means the most efficient manner to add Ajax capabilities to your site. We are requesting a rather large blob over the wire when having the server do the rendering for us. Another option would be to request only the data and then format it on the client-side, which we will cover in the next recipe.