jQuery UI Accordion in ASP.NET MVC - feed with data from database
2011-10-29 16:29
477 查看
In the part one I’ve shown how to implement the jQuery Accordion on client-side and add some predefined skin.
In the part two the Accordion is implemented in ASP.NET WebForms application, filled with data from database.
Now, in this blog I will show the same for an ASP.NET MVC application.
Note: The database table is the same from the part 2.[/i]
I’ve created empty ASP.NET MVC Application.
I’ve added the following
So, I have one HomeController, one Home –> Accordion.aspx View and WebStoreModel.edmx entity model for the WebStore database. It contains only one table.
Then, in the Global.asax file I’ve slightly changed the default route
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Accordion", id = UrlParameter.Optional } // Parameter defaults
);
}
since I don’t have Index.aspx page, I only have Accordion.aspx page.
In the HomeController.cs I have the following ActionResult method
using jQueryUIAccordion_MVC.Models;
public ActionResult Accordion()
{
var entities = new MyWebStoreEntities();
return View(entities.Products.ToList());
}
MyWebStoreEntities()[/b] is the entity container name. This way, if we would have more entities inside the model, we can easily retrieve any.
You can use LINQ 2 SQL Classes if you want and you will retrieve the data using LINQ statement.
And, the Accordion View code is
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>jQuery Accordion :: ASP.NET MVC Example</title>
<link type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/themes/blitzer/jquery-ui.css" rel="Stylesheet" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.6/jquery-ui.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$("#products").accordion();
});
</script>
</head>
<body>
<div id="products" style="width: 500px;">
<% foreach (var item in Model) { %>
<h3>
<a href="#"><%: item.name %></a></h3>
<div>
<p>
<%: item.description %>
</p>
<p>
<font color="green">Price <b><%: item.price %></b></font>
</p>
</div>
<% } %>
</div>
</body>
</html>
Note: You can use <%: item.name %> if you have ASP.NET 4.0 which replaces the Html.Encode(item.name) – otherwise, you will need to go with <%= Html.Encode(item.name) %> or Server.HtmlEncode(..).
And that’s it. If you run the web application, the result will be same as in part 2.
Great result in few simple steps! Thats what you get if you use jQuery with ASP.NET ;)!
I hope you like it :).
Regards,
Hajan
YOU CAN DOWNLOAD THE COMPLETE PROJECT (Client-side Implementation + WebForms + MVC) HERE.
In the part two the Accordion is implemented in ASP.NET WebForms application, filled with data from database.
Now, in this blog I will show the same for an ASP.NET MVC application.
Note: The database table is the same from the part 2.[/i]
I’ve created empty ASP.NET MVC Application.
I’ve added the following
So, I have one HomeController, one Home –> Accordion.aspx View and WebStoreModel.edmx entity model for the WebStore database. It contains only one table.
Then, in the Global.asax file I’ve slightly changed the default route
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Accordion", id = UrlParameter.Optional } // Parameter defaults
);
}
since I don’t have Index.aspx page, I only have Accordion.aspx page.
In the HomeController.cs I have the following ActionResult method
using jQueryUIAccordion_MVC.Models;
public ActionResult Accordion()
{
var entities = new MyWebStoreEntities();
return View(entities.Products.ToList());
}
MyWebStoreEntities()[/b] is the entity container name. This way, if we would have more entities inside the model, we can easily retrieve any.
You can use LINQ 2 SQL Classes if you want and you will retrieve the data using LINQ statement.
And, the Accordion View code is
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>jQuery Accordion :: ASP.NET MVC Example</title>
<link type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/themes/blitzer/jquery-ui.css" rel="Stylesheet" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.6/jquery-ui.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$("#products").accordion();
});
</script>
</head>
<body>
<div id="products" style="width: 500px;">
<% foreach (var item in Model) { %>
<h3>
<a href="#"><%: item.name %></a></h3>
<div>
<p>
<%: item.description %>
</p>
<p>
<font color="green">Price <b><%: item.price %></b></font>
</p>
</div>
<% } %>
</div>
</body>
</html>
Note: You can use <%: item.name %> if you have ASP.NET 4.0 which replaces the Html.Encode(item.name) – otherwise, you will need to go with <%= Html.Encode(item.name) %> or Server.HtmlEncode(..).
And that’s it. If you run the web application, the result will be same as in part 2.
Great result in few simple steps! Thats what you get if you use jQuery with ASP.NET ;)!
I hope you like it :).
Regards,
Hajan
YOU CAN DOWNLOAD THE COMPLETE PROJECT (Client-side Implementation + WebForms + MVC) HERE.
相关文章推荐
- ASP.NET MVC #01, Create a Movie Database Application in 15 Minutes with ASP.NET MVC
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- EF Database First with ASP.NET MVC: Creating the Web Application and Data Models开发遇到问题的解决办法
- [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4
- Three steps to use jQuery UI in ASP.NET MVC 5
- Working with Data in ASP.NET 2.0 :: Using TemplateFields in the GridView Control
- ASP.NET MVC 3 (Accessing your Model's Data from a Controller) (5/9)
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- Displaying a Sorted, Paged, and Filtered Grid of Data in ASP.NET MVC
- Can we call the Method of a controller from another controller in asp.net MVC?
- 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像
- Working with Data in ASP.NET 2.0 :: Master Pages and Site Navigation
- 基于ASP.NET MVC 4、WebApi、jQuery和FormData的多文件上传方法
- Authorize attribute and jquery AJAX in asp.net MVC
- Using JQuery to perform Ajax calls in ASP.NET MVC
- jQuery Templates with ASP.NET MVC
- Implementing & Consuming ASP.NET WEB API from JQuery (MVC 4)
- jQuery Grid With ASP.Net MVC
- ASP.NET MVC 3 (Accessing your Model's Data from a Controller) (5/9)
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData