2017-06-02 105 views
1

我正在開發一個在線商店的web項目。目前,我試圖在網頁上顯示數據庫表格中的內容。使用asp.net mvc爲數據庫創建Web界面5

表看起來是這樣的: image

這是型號:

using System; 
using System.ComponentModel.DataAnnotations; 

namespace Vidly.Models 
{ 
    public class Rental 
    { 
     public int Id { get; set; } 

     [Required] 
     public Customer Customer { get; set; } 

     [Required] 
     public Movie Movie { get; set; } 

     public DateTime DateRented { get; set; } 

     public DateTime? DateReturned { get; set; } 
    } 
} 

我創建了一個API控制器,它看起來像這樣

using AutoMapper; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using Vidly.Dtos; 
using Vidly.Models; 
using System.Data.Entity; 


namespace Vidly.Controllers.Api 
{ 
    public class RentalsController : ApiController 
    { 

     private ApplicationDbContext _context; 

     public RentalsController() 
     { 
      _context = new ApplicationDbContext(); 
     } 

     // GET /api/rentals 
     public IHttpActionResult GetRentals(string query = null) 
     { 
      var rentalsQuery = _context.Rentals 
       .Include(r => r.Customer) 
       .Include(r => r.Movie); 

      var rentalDtos = rentalsQuery 
       .ToList() 
       .Select(Mapper.Map<Rental, RentalDto>); 

      return Ok(rentalDtos); 
     } 

     // GET /api/rentals/1 
     public IHttpActionResult GetRental(int id) 
     { 
      var rental = _context.Rentals.SingleOrDefault(r => r.Id == id); 

      if (rental == null) 
       return NotFound(); 

      return Ok(Mapper.Map<Rental, RentalDto>(rental)); 
     } 
    } 
} 

,另一個控制器,看起來像這樣:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Vidly.Models; 
using System.Data.Entity; 
using Vidly.ViewModels; 

namespace Vidly.Controllers 
{ 
    public class RentalsController : Controller 
    { 
     private ApplicationDbContext _context; 

     [Authorize(Roles = RoleName.CanManageMovies)] 
     public ViewResult Index() 
     { 
      if (User.IsInRole(RoleName.CanManageMovies)) 
       return View("Index"); 
      else 
       return View("Home"); 
     } 
     public ActionResult New() 
     { 
      return View(); 
     } 

     public ActionResult Details(int id) 
     { 
      var rental = _context.Rentals.Include(r => r.Movie).Include(r => r.Customer).SingleOrDefault(r => r.Id == id); 

      if (rental == null) 
       return HttpNotFound(); 

      return View(rental); 
     } 

    } 
} 

最後,CSHTML文件看起來是這樣的:

@model IEnumerable<Vidly.Models.Rental> 
@{ 
    ViewBag.Title = "Rentals"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2>Rentals</h2> 

<table id="rentals" class="table table-bordered table-hover"> 
    <thead> 
     <tr> 
      <th>Customer</th> 
      <th>Movie</th> 
      <th>Date Rented</th> 
     </tr> 
    </thead> 
    <tbody></tbody> 
</table> 

@section scripts 
{ 
    <script> 
     $(document).ready(function() { 
      var table = $("#rentals").DataTable({ 
       ajax: { 
        url: "/api/rentals", 
        dataSrc: "" 
       }, 
       columns: [ 
        { 
         data: "customer.name" 
        }, 
        { 
         data: "movie.name" 
        }, 
        { 
         data: "daterented" 
        } 
       ] 
      }); 

     }); 
    </script> 
} 

的問題是,每當我試圖訪問該網頁時,我收到此錯誤消息5次,在每個條目表:

數據表警告:表ID =出租 - 請求的未知參數 「daterented」爲行0,列2。有關此 錯誤的詳細信息,請參閱http://datatables.net/tn/4

表看起來是這樣的: image

和XML文件(訪問https://localhost:44300/api/rentals可用時),從那裏對錶中的數據將被取回這個樣子的: image

我會很很高興能幫助我解決這個問題! 非常感謝!

+0

你能告訴我們什麼是從服務器的響應。您可以在網絡標籤 –

+0

中檢查。這是一個鏈接到它的圖像。 https://ibb.co/nmFO2a – Adrian

+0

那麼你正在調用這個方法:'/ api/rentals',但我看不到它。這應該是正確的方法:'GetRentals'? –

回答

0

好吧,我已經啓用了Camelcase。

https://en.wikipedia.org/wiki/Camel_case

因此,代替

columns: [ 
       { 
        data: "customer.name" 
       }, 
       { 
        data: "movie.name" 
       }, 
       { 
        data: "daterented" 
       } 
      ] 

我的代碼應該已經

columns: [ 
       { 
        data: "customer.name" 
       }, 
       { 
        data: "movie.name" 
       }, 
       { 
        data: "dateRented" 
       } 
      ]