繁体   English   中英

ASP.NET MVC显示数据库表

[英]ASP.NET MVC displaying database table

我刚刚开始学习MVC 4 ASP.NET。 我将使用名为MVC音乐商店的教程作为起点。 我有一个名为“ Orders”的数据库表,其中包含许多记录。 我决定尝试在表格中显示所有订单。 这是订单模型:

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcMusicStore.Models
{
[Bind(Exclude = "OrderId")]
public partial class Order
{
    [ScaffoldColumn(false)]
    public int OrderId { get; set; }

    [ScaffoldColumn(false)]
    public System.DateTime OrderDate { get; set; }

    [ScaffoldColumn(false)]
    public string Username { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    [DisplayName("First Name")]
    [StringLength(160)]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required")]
    [DisplayName("Last Name")]
    [StringLength(160)]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Address is required")]
    [StringLength(70)]
    public string Address { get; set; }

    [Required(ErrorMessage = "City is required")]
    [StringLength(40)]
    public string City { get; set; }

    [Required(ErrorMessage = "State is required")]
    [StringLength(40)]
    public string State { get; set; }

    [Required(ErrorMessage = "Postal Code is required")]
    [DisplayName("Postal Code")]
    [StringLength(10)]
    public string PostalCode { get; set; }

    [Required(ErrorMessage = "Country is required")]
    [StringLength(40)]
    public string Country { get; set; }

    [Required(ErrorMessage = "Phone is required")]
    [StringLength(24)]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Email Address is required")]
    [DisplayName("Email Address")]
    [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
        ErrorMessage = "Email is is not valid.")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [ScaffoldColumn(false)]
    public decimal Total { get; set; }

    public List<OrderDetail> OrderDetails { get; set; }
   }
   }

我可以使用剃刀视图显示数据库结果,但是它会在一个很长的表的一页上显示每个视图。

我的问题是,有没有一种方法可以使该页面仅显示20个订单,然后单击下一页以查看下20个订单? 就像您知道整个过程的工作原理一样,我只是使用实体框架进行“代码优先”操作(Microsoft在大多数教程中似乎都鼓励使用这种方法):

using System.Data.Entity;

namespace MvcMusicStore.Models
{
public class MusicStoreEntities : DbContext
{
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
}
}

控制器:

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

namespace Test.Controllers
{
public class OrdersController : Controller
{
    private MusicStoreEntities db = new MusicStoreEntities();

    //
    // GET: /Orders/

    public ActionResult Index()
    {
        return View(db.Orders.ToList());
    }


}
}

这是剃刀视图:

@model IEnumerable<MvcMusicStore.Models.Order>

@{
ViewBag.Title = "Index";
}


<h2>Orders Overview</h2>
<br />
<div class="CSSTableGenerator" >
<table>
<tr>
    <td>
        @Html.DisplayNameFor(model => model.OrderId)
    </td>
     <td>
        @Html.DisplayNameFor(model => model.OrderDate)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Username)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Total)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.FirstName)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.LastName)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Address)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.City)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.State)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.PostalCode)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Country)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Phone)
    </td>
    <td>
        @Html.DisplayNameFor(model => model.Email)
    </td>


</tr>

@foreach (var item in Model) {
<tr>
     <td>
        @Html.DisplayFor(modelItem => item.OrderId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.OrderDate)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Username)
    </td>
      <td>
        @Html.DisplayFor(modelItem => item.Total)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Address)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.City)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.State)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PostalCode)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Country)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Phone)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>

        @Html.ActionLink("Details", "Details", new { id=item.OrderId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.OrderId })
    </td>
    </tr>
    }

    </table>
    </div>

第二个问题是,表中的每个订单的总价格始终为0.0。 但是所有其他字段都是正确的。 感谢您的提示。

关于您的第一个问题...分页是您想要完成的任务,并且有一些很好的指南介绍了如何完成此任务。 就个人而言,我建议学习Linq / Entity Framework / MVC Framework的用法,因为一旦您知道这些更好一点,则分页位应该很容易添加。

分页教程

关于第二个问题...

您可以使用服务器资源管理器来检查数据库的内容,这是我将开始验证Total实际存在的地方!

服务器浏览器

为此,您可能必须使用“添加连接”实用程序(如果尚未设置),该实用程序如图所示在“数据”显示中的“ t”正上方。

暂无
暂无

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

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