locked
DATATABLE FOR INDEX VIEW , ASP.NET MVC5 APP RRS feed

  • Question

  • User-1473047469 posted

    HI.this is the view and the controller :how can anyone help me show the results into datatableE :

    @model IEnumerable<Northwind.Order>
    
    @{
        ViewBag.Title = "";
        Layout = "~/Views/Shared/MasterDetailsLayoutPage.cshtml";
    }
    
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EmployeeID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.OrderDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequiredDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShippedDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Freight)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipName)
            </th>
          
         
          
            <th>
                @Html.DisplayNameFor(model => model.ShipCountry)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Customer.CompanyName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Employee.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Shipper.CompanyName)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.ActionLink(item.CustomerID, "Details", new { id = item.OrderID })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OrderDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RequiredDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShippedDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Freight)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipName)
            </td>
            
          
          
            <td>
                @Html.DisplayFor(modelItem => item.ShipCountry)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Customer.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Employee.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Shipper.CompanyName)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.OrderID }) |
                @Html.ActionLink("Details", "Details", new { id=item.OrderID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.OrderID })
            </td>
        </tr>
    }
    
    </table>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Northwind;
    
    namespace Northwind.Controllers
    {
        public class OrdersController : Controller
        {
            private dbNorthwindEntities db = new dbNorthwindEntities();
    
            // GET: Orders
            public ActionResult Index()
            {
                var orders = db.Orders.Include(o => o.Customer).Include(o => o.Employee).Include(o => o.Shipper);
                return View(orders.ToList());
            }
    
            // GET: Orders/Details/5
            public ActionResult Details(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Order order = db.Orders.Find(id);
                if (order == null)
                {
                    return HttpNotFound();
                }
                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
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] Order order)
            {
                if (ModelState.IsValid)
                {
                    db.Orders.Add(order);
                    db.SaveChanges();
                    return RedirectToAction("Details" , new { id = order.OrderID });
                }
    
                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)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Order order = db.Orders.Find(id);
                if (order == null)
                {
                    return HttpNotFound();
                }
                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
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] 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)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Order order = db.Orders.Find(id);
                if (order == null)
                {
                    return HttpNotFound();
                }
                return View(order);
            }
    
            // POST: Orders/Delete/5
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            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)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }
    

    Monday, January 15, 2018 9:21 AM

Answers

All replies