繁体   English   中英

使用asp.net mvc 5为数据库创建Web界面

[英]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。

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"
                }
            ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM