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