[英]Creating a web interface for a database using asp.net mvc 5
我正在開發一個在線商店網絡項目。 目前,我正在嘗試在網頁上顯示數據庫表中的內容。
該表如下所示:
這是模型:
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次,對於表中的每個條目:
DataTables警告:table id = rental - 第0行第2列請求的未知參數'daterented'。有關此錯誤的更多信息,請參閱http://datatables.net/tn/4
該表如下所示:
從應該獲取表的數據的XML文件(訪問https:// localhost:44300 / api / rental時可用)如下所示:
如果你能解決這個問題,我會很高興的! 非常感謝!
好吧,我似乎啟用了Camelcase。
因此,而不是
columns: [
{
data: "customer.name"
},
{
data: "movie.name"
},
{
data: "daterented"
}
]
我的代碼應該是
columns: [
{
data: "customer.name"
},
{
data: "movie.name"
},
{
data: "dateRented"
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.