【发布时间】:2017-11-03 22:37:42
【问题描述】:
我正在开发一个在线商店网络项目。目前,我正在尝试在网页上显示数据库表中的内容。
表格如下所示:
这是模型:
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=rentals - 请求的未知参数 'daterented' 表示第 0 行第 2 列。有关此内容的更多信息 错误,请看http://datatables.net/tn/4
表格如下所示:
应该从中获取表数据的 XML 文件(访问 https://localhost:44300/api/rentals 时可用)如下所示:
如果您能帮我解决这个问题,我将非常高兴! 非常感谢!
【问题讨论】:
-
你能告诉我们服务器的响应是什么吗?您可以在网络标签中查看它
-
是的。这是它的图像的链接。 ibb.co/nmFO2a
-
好吧,您正在调用此方法:
/api/rentals但我看不到它。这应该是正确的方法:GetRentals? -
API 响应没有属性
customer.name或movie.name。如果这就是您看到此错误的原因,则数据表无法在数据源中找到这些。推荐的方式是使用 json 格式从 API 返回数据,以便 jQuery 轻松解析。 -
修复了这个问题。更新了帖子。仍然无法显示“daterented”。
标签: c# ajax entity-framework razor asp.net-mvc-5