Advertisement
PanuSy

CustomerController

Feb 26th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using NorthwindData.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7.  
  8. namespace NorthwindData.Controllers
  9. {
  10.     public class CustomerController : Controller
  11.     {
  12.         // GET: Customer
  13.         public ActionResult Index()
  14.         {
  15.             List<Customers> model = new List<Customers>();
  16.             try
  17.             {
  18.                 northwindEntities entities = new northwindEntities();
  19.                 model = entities.Customers.ToList();
  20.                 entities.Dispose();
  21.             }
  22.             catch (Exception ex)
  23.             {
  24.                 ViewBag.ErrorMessage = ex.GetType() + ": " + ex.Message;
  25.             }
  26.  
  27.             return View(model);
  28.         }
  29.  
  30.         public ActionResult GetOrders(string id)
  31.         {
  32.             northwindEntities entities = new northwindEntities();
  33.             List<Orders> orders = (from o in entities.Orders
  34.                                    where o.CustomerID == id
  35.                                    select o).ToList();
  36.             entities.Dispose();
  37.  
  38.             List<SimpleOrderData> result = new List<SimpleOrderData>();
  39.             foreach (Orders order in orders)
  40.             {
  41.                 SimpleOrderData data = new SimpleOrderData();
  42.                 data.CustomerId = order.CustomerID;
  43.                 data.OrderId = order.OrderID;
  44.                 data.OrderDate = order.OrderDate.ToString();
  45.                 result.Add(data);
  46.             }
  47.  
  48.             return Json(result, JsonRequestBehavior.AllowGet);
  49.         }
  50.  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement