If you haven't already installed ASP.NET MVC 3 use this resource: http://www.asp.net/mvc/mvc3
For this tutorial you're also going to need the Entity Framework: http://www.microsoft.com/download/en/details.aspx?id=18504
For our purpose we will use the Northwind database which you can download from here. So let's begin!
Create new ASP.NET MVC 3 project and choose the "Empty project" option for template. For "View engine" select "Razor".
<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="http://jqwidgets.com/jquery-widgets-demo/scripts/jquery-1.11.1.min.js" type="text/javascript"></script> <link href="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/styles/jqx.base.css" rel="stylesheet" type="text/css" /> <script src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqx-all.js" type="text/javascript"></script></head><body> @RenderBody()</body></html>
using System;using System.Collections.Generic;using System.Data;using System.Data.Entity;using System.Linq;using System.Web;using System.Web.Mvc;using Project.Models;namespace Project.Controllers{ public class OrdersController : Controller { private NORTHWNDEntities db = new NORTHWNDEntities(); // // GET: /Orders/ public ViewResult Index() { var orders = db.Orders.Include(o => o.Customer).Include(o => o.Employee).Include(o => o.Shipper); return View(orders.ToList()); } public JsonResult GetOrders() { var orders = db.Orders.ToList<Order>(); var orderDetails = db.Order_Details.ToList<Order_Detail>(); var products = db.Products.ToList<Product>(); var result = (from d in orderDetails join o in orders on d.OrderID equals o.OrderID join p in products on d.ProductID equals p.ProductID select new { o.OrderDate, d.Quantity, p.ProductName }).Take(50); return Json(result, JsonRequestBehavior.AllowGet); } // // GET: /Orders/Details/5 public ViewResult Details(int id) { Order order = db.Orders.Find(id); return View(order); } // // GET: /Orders/Create public ActionResult Create() { ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName"); ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName"); ViewBag.ShipVia = new SelectList(db.Shippers, "ShipperID", "CompanyName"); return View(); } // // POST: /Orders/Create [HttpPost] public ActionResult Create(Order order) { if (ModelState.IsValid) { db.Orders.Add(order); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", order.CustomerID); ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", order.EmployeeID); ViewBag.ShipVia = new SelectList(db.Shippers, "ShipperID", "CompanyName", order.ShipVia); return View(order); } // // GET: /Orders/Edit/5 public ActionResult Edit(int id) { Order order = db.Orders.Find(id); ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", order.CustomerID); ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", order.EmployeeID); ViewBag.ShipVia = new SelectList(db.Shippers, "ShipperID", "CompanyName", order.ShipVia); return View(order); } // // POST: /Orders/Edit/5 [HttpPost] public ActionResult Edit(Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", order.CustomerID); ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", order.EmployeeID); ViewBag.ShipVia = new SelectList(db.Shippers, "ShipperID", "CompanyName", order.ShipVia); return View(order); } // // GET: /Orders/Delete/5 public ActionResult Delete(int id) { Order order = db.Orders.Find(id); return View(order); } // // POST: /Orders/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Order order = db.Orders.Find(id); db.Orders.Remove(order); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } }}
<script type="text/javascript"> $(document).ready(function () { var source = { datatype: "json", datafields: [ { name: 'OrderDate', type: 'date' }, { name: 'Quantity' }, { name: 'ProductName' } ], url: 'Orders/GetOrders' }; var dataAdapter = new $.jqx.dataAdapter(source, { autoBind: true, async: false, downloadComplete: function () { }, loadComplete: function () { }, loadError: function () { } }); // prepare jqxChart settings var settings = { title: "Orders by Date", showLegend: true, padding: { left: 5, top: 5, right: 40, bottom: 5 }, titlePadding: { left: 90, top: 0, right: 0, bottom: 10 }, source: dataAdapter, categoryAxis: { text: 'Category Axis', textRotationAngle: 0, dataField: 'OrderDate', formatFunction: function (jsonDate) { return $.jqx.dataFormat.formatdate(jsonDate, "MM-yyyy"); }, showTickMarks: true, tickMarksInterval: 10, tickMarksColor: '#888888', unitInterval: 10, showGridLines: true, gridLinesInterval: 10, gridLinesColor: '#888888', axisSize: 'auto' }, colorScheme: 'scheme05', seriesGroups: [ { type: 'line', valueAxis: { description: 'Quantity', axisSize: 'auto', tickMarksColor: '#888888', unitInterval: 20, minValue: 0, maxValue: 100 }, series: [ { dataField: 'Quantity', displayText: 'Quantity' } ] } ] }; $('#jqxChart').jqxChart(settings); });</script><div id="jqxChart" style="width: 600px; height: 400px;"></div>
public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Orders", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Orders", action = "Index", id = UrlParameter.Optional } // Parameter defaults );}