Javascript đặt thuộc tính ViewModel

Nhiều nhà phát triển biết rằng họ có thể tạo biểu mẫu trên các trang web với mã tối thiểu bằng ASP. NET mô hình ràng buộc. Các mẫu chế độ xem MVC mặc định của Visual Studio thậm chí sẽ tạo một danh sách tiêu chuẩn, tạo, chỉnh sửa và xóa các chế độ xem mà không cần bất kỳ chương trình bổ sung nào

Nhưng sức mạnh của liên kết mô hình mặc định vượt ra ngoài mô hình dữ liệu phẳng của biểu mẫu đầu vào đơn giản hoặc danh sách bản ghi. Sử dụng một số kỹ thuật mã hóa đơn giản, các nhà phát triển có thể sử dụng ASP. NET để tạo biểu mẫu và thu thập dữ liệu cho các mối quan hệ thực thể phân cấp. Trong nhiều ứng dụng, điều này có thể tạo ra sự khác biệt giữa việc tận dụng khả năng phát triển nhanh chóng của ASP. NET MVC và gắn kết với cơ sở hạ tầng bổ sung và độ phức tạp của khung phía máy khách như Angular hoặc React

Hướng dẫn này sẽ trình bày một ví dụ về việc sử dụng ASP. NET MVC liên kết để trình bày và thu thập dữ liệu biểu mẫu phân cấp trong cấu trúc phân cấp

Cấp độ kỹ năng

Sẽ rất hữu ích khi hiểu về các chủ đề này ở cấp độ kỹ năng được chỉ định

Công nghệTrình độ kỹ năngASP. NETIntermediateC#IntermediateEntity FrameworkBeginnerMVCIntermediateMVVMBeginner

Phạm vi

Hướng dẫn này sẽ trình bày một ví dụ về việc sử dụng ASP. NET MVC liên kết để trình bày và thu thập dữ liệu biểu mẫu phân cấp trong cấu trúc phân cấp

Dự án ví dụ và mã được trình bày trong hướng dẫn này dựa trên. Nền tảng NET. Chi tiết triển khai cho. NET Core và. NET Standard sẽ được trình bày trong một hướng dẫn khác

Cấu trúc

  1. Chúng ta sẽ bắt đầu với tổng quan về các thực thể nghiên cứu điển hình và các quan điểm chính của giải pháp ví dụ được sử dụng để chuẩn bị cho hướng dẫn này

  2. Sau đó, chúng ta sẽ xem cách tạo một mô hình khung nhìn kết hợp các trường thành viên thuộc nhiều kiểu nguyên thủy khác nhau và kết hợp một trường là tập hợp của một kiểu đối tượng

  3. Tiếp theo, chúng ta sẽ xem mã cần thiết để trình bày thông tin cho người dùng cuối

  4. Chúng ta sẽ kết thúc bằng cách xem xét kỹ cách đảm bảo rằng mã Razor tạo đúng HTML và chúng ta sẽ xem cách sử dụng HtmlHelpers và CSS để áp dụng định dạng cho các trường biểu mẫu được tạo bởi dạng xem

nghiên cứu điển hình

Mã và ảnh chụp màn hình được hiển thị trong hướng dẫn này khớp với mã và xem bố cục một dự án Visual Studio mẫu. Giải pháp có thể được rẽ nhánh hoặc tải xuống từ kho lưu trữ GitHub

github. com/ajsaulsberry/BlipBinding

Giải pháp mẫu thực hiện như sau

  • Một giải pháp đa dự án với các dự án riêng biệt cho

    • Ứng dụng web
    • thực thể
    • Các lớp dữ liệu (bối cảnh và kho lưu trữ)
  • Mẫu thiết kế Model-View ViewModel (MVVM)
  • Mẫu thiết kế kho lưu trữ
  • Entity Framework ORM với sự phát triển mã đầu tiên

Sử dụng giải pháp ví dụ, bạn có thể làm theo từng phần bên dưới và tự mình thử nghiệm

điều kiện tiên quyết

bạn nên có

  • Kiến thức làm việc về ASP. NET MVC
  • Hiểu biết về mẫu thiết kế Model View ViewModel (MVVM)
  • Visual Studio đã sẵn sàng hoạt động

Giải pháp nghiên cứu trường hợp BlipBinding

Nghiên cứu điển hình được thực hiện bởi giải pháp BlipBinding là một ứng dụng đơn giản để duy trì thông tin về khách hàng, đơn đặt hàng của họ và các mặt hàng trong đơn đặt hàng của họ. Kho lưu trữ dữ liệu vĩnh viễn cho ứng dụng là cơ sở dữ liệu SQL Server. Các bảng và mối quan hệ của chúng được hiển thị bên dưới

Javascript đặt thuộc tính ViewModel

Sơ đồ mối quan hệ thực thể cơ sở dữ liệu BlipBinding

Thực thể và Mối quan hệ

Lưu ý rằng mối quan hệ nhiều-nhiều giữa Đơn đặt hàng và Mặt hàng được triển khai thông qua việc sử dụng bảng hợp nhất có trọng tải. Ngoài việc duy trì mối quan hệ giữa Đơn đặt hàng và Mặt hàng, bảng OrderItems còn chứa thông tin về các mặt hàng có trong đơn đặt hàng, giá bán và số lượng mặt hàng đã bán.

Rõ ràng, đây không phải là một hệ thống xử lý đơn hàng hoàn chỉnh;

Cơ sở dữ liệu được tạo và duy trì bằng cách sử dụng thiết kế đầu tiên mã Entity Framework. Mỗi bảng và mối quan hệ của nó với các bảng khác được định nghĩa bởi một lớp trong Blip. Dự án thực thể của giải pháp BlipBinding. Hãy xem xét các thực thể

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
0 và
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1
.

đốm sáng. Thực thể\Khách hàng\Khách hàng. cs

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}

csharp

Lưu ý các đặc điểm sau

  • ComponentModel DataAnnotations được sử dụng để xác định trường khóa cho cơ sở dữ liệu và để chỉ định kích thước cũng như các tùy chọn khác

  • Mối quan hệ một-nhiều giữa Khách hàng và Đơn đặt hàng được tạo bởi trường thành viên

    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    2 bao gồm một tập hợp
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    1
    entities.

  • Mối quan hệ bắt buộc giữa Khách hàng và Quốc gia trong cơ sở dữ liệu được phản ánh trong trường để giữ giá trị
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    4
    và trường để xác định mối quan hệ .
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    5
    , which is of type
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    5
    .

Trật tự. cs

Thực thể

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1 được xác định theo cách tương tự.

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}

csharp

Lưu ý rằng

  • The

    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    1 chỉ có thể thuộc về một
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    0
    , .
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    20
    and the navigation property
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    0
    .

  • Mối quan hệ một-nhiều giữa Đơn đặt hàng và Mặt hàng được triển khai thông qua thuộc tính ảo để tập hợp các thực thể
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    22
    .

Bối cảnh dữ liệu và di chuyển đầu tiên mã Entity Framework cho cơ sở dữ liệu được đặt trong Blip. Dự án dữ liệu, cùng với các phương pháp kho lưu trữ

trình bày dữ liệu

Blip. Dự án web trong giải pháp BlipBinding dựa trên tiêu chuẩn. NET Framework MVC, vì vậy bố cục của các dạng xem dựa trên kiểu CSS Bootstrap mặc định và

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
23 đi kèm với mẫu.

Đối với mục đích của hướng dẫn này, có hai chế độ xem đáng chú ý trong nghiên cứu điển hình, một để hiển thị danh sách khách hàng và một để hiển thị danh sách đơn đặt hàng cho từng khách hàng

Khách hàng/Chế độ xem chỉ mục

Chế độ xem cho danh sách khách hàng là một bảng đơn giản hiển thị một số thông tin cơ bản về khách hàng và một

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
24 phương pháp trợ giúp để điều hướng đến .

Javascript đặt thuộc tính ViewModel

Giải pháp BlipBinding, Blip. Dự án web, chế độ xem Khách hàng/Chỉ mục

Bằng cách sử dụng phương pháp

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
25 di chuyển mã đầu tiên của Entity Framework, chúng tôi đã điền vào cơ sở dữ liệu một vài khách hàng, đơn đặt hàng và mặt hàng. Nếu bạn chạy giải pháp ví dụ, ứng dụng sẽ tạo cơ sở dữ liệu và thêm các bản ghi giống nhau (GUID do máy tính của bạn tạo cho các trường khóa sẽ khác với các trường được hiển thị).

đốm sáng. Web\Lượt xem\Khách hàng\Chỉ mục. cshtml

Trong mã cho chế độ xem ở trên, lưu ý rằng danh sách khách hàng được tạo bằng một

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
26 vòng lặp lặp qua bộ sưu tập < . Đây là cách tiêu chuẩn để trình bày danh sách có số lượng bản ghi thay đổi.
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
27
entities. This is a standard way of presenting a list with a variable number of records:

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}

csharp

Trong danh sách khách hàng, view model có cấu trúc phẳng, nó chỉ là một danh sách liệt kê các đối tượng chứa thông tin khách hàng. Đây là chỉ thị ______128 từ đầu

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
29 .

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
2

csharp

Bây giờ chúng ta hãy xem xét kỹ hơn về mô hình chế độ xem đó

đốm sáng. Thực thể\Khách hàng. ViewModels\CustomerDisplayViewModel. cs

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
4

csharp

Lưu ý rằng chúng tôi đang sử dụng chú thích dữ liệu trong mô hình chế độ xem để cung cấp nhãn trường để hiển thị trên chế độ xem

Khi phương thức kho lưu trữ điền vào mô hình này, nó sẽ kết hợp dữ liệu từ bảng Khách hàng với CountryNameEnglish từ bảng Quốc gia và Tên vùng từ bảng Khu vực. Theo cách này, mô hình khung nhìn có thể trình bày thông tin hữu ích hơn cho người dùng so với các giá trị chỉ mục cho quốc gia và khu vực từ bảng Khách hàng

Đặt hàng/Chế độ xem chỉ mục

Danh sách đơn đặt hàng đơn giản cho khách hàng hiển thị thông tin khách hàng, số và ngày đặt hàng dưới dạng các trường chỉ đọc và đơn đặt hàng/mô tả dưới dạng trường có thể chỉnh sửa. Bằng cách thay đổi các giá trị trong trường có thể chỉnh sửa và lưu, chúng ta có thể thấy cách hoạt động của liên kết mô hình khi thực hiện các hành động HttpPost

Javascript đặt thuộc tính ViewModel

Giải pháp BlipBinding, Blip. Dự án web, Chế độ xem Đơn hàng/Chỉ mục

Theo quan điểm này, chúng tôi đang trình bày thông tin theo cấu trúc phân cấp. Ở cấp cao nhất là thông tin khách hàng. Bên dưới đó là danh sách các đơn đặt hàng cho khách hàng

Hãy xem cách dữ liệu được trình bày trong mã

đốm sáng. Web\Lượt xem\Đặt hàng\Chỉ mục. cshtml

Đối với dữ liệu cấp cao nhất, liên quan đến khách hàng, các trường được sắp xếp theo cách rất chuẩn

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
5

csharp

Lưu ý rằng chúng tôi đang sử dụng

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
40 Trình trợ giúp Html để cho phép công cụ Dao cạo xác định loại phần tử HTML chính xác cho loại dữ liệu. Chúng tôi cũng đang áp dụng lớp CSS ____241 để đảm bảo điều khiển chọn kiểu phù hợp. Trường được thay đổi từ hộp văn bản có thể chỉnh sửa thành trường chỉ hiển thị bằng cách áp dụng thuộc tính HTML
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
42
.

Đối với dữ liệu tầng thứ hai, danh sách đơn đặt hàng, chúng tôi đang lặp qua các bản ghi trong mô hình dạng xem. Nhưng trong trường hợp này, chúng tôi không sử dụng vòng lặp ____126 và chúng tôi không sử dụng

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
40 HtmlHelper. We'll look at the reasons for these choices in more detail in the section on saving data.

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1

csharp

Bạn cũng sẽ lưu ý rằng chúng ta đang sử dụng vòng lặp

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
45 với biến đếm thay vì
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
26
loop. This is crucial to getting binding to work for the 'HttpPost' action, as we'll see soon.

đốm sáng. Thực thể\Đơn hàng. ViewModels\CustomerOrdersDisplayViewModel. cs

Mô hình chế độ xem cho đơn đặt hàng của khách hàng phản ánh cấu trúc phân cấp của chế độ xem được hiển thị ở trên. Nó tập hợp thông tin hiển thị về khách hàng từ các bảng Khách hàng, Quốc gia và Khu vực, đồng thời bao gồm một thuộc tính là tập hợp các thực thể

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
47 .

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
5

csharp

Hãy xem lớp tạo nên bộ sưu tập ____248 .

đốm sáng. Thực thể\Đơn hàng. ViewModels\OrderDisplayViewModel. cs

Lưu ý rằng mỗi thực thể trong

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
47 được liên kết với khách hàng được liên kết trong
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
50
. When we transpose the entities into the view model structure we need to preserve the relationship between the entities (and the tables in the database).

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
9

csharp

Cũng lưu ý rằng không có thuộc tính

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
2 nào trong một trong hai lớp này để cung cấp điều hướng giữa các thực thể. Các mô hình khung nhìn phục vụ mục đích chức năng của khung nhìn và được tách rời khỏi các mối quan hệ thực thể của các lớp và các bảng cơ sở dữ liệu. Theo đó, khi hai mô hình khung nhìn được sử dụng cùng nhau, chúng phản ánh (các) mối quan hệ giữa các mô hình khung nhìn, thay vì các thực thể mà dữ liệu của chúng được lấy ra từ đó.

Các phương thức kho lưu trữ đảm nhiệm việc chuyển đổi dữ liệu từ cấu trúc của các thực thể sang cấu trúc của các mô hình khung nhìn và ngược lại

Hành động OrderController cho Index HttpGet

Bằng cách sử dụng MVVM và mẫu thiết kế kho lưu trữ, chúng tôi có thể làm cho các hành động của bộ điều khiển của mình ngắn gọn và cung cấp sự phân tách các mối quan tâm giữa lớp trình bày, logic nghiệp vụ và dữ liệu. Chúng ta có thể thấy điều đó đang hoạt động trong hành động của bộ điều khiển cư trú trong chế độ xem Đơn hàng/Chỉ mục

đốm sáng. Web\Controllers\OrderController. cs

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1

csharp

Tất cả những gì hành động của bộ điều khiển này cần thực hiện khi được chuyển một

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
20 từ chế độ xem Khách hàng/Chỉ mục là chuyển giá trị đó sang phương thức lưu trữ thích hợp .
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
50
class, and pass it to the view.

Lưu dữ liệu

Lưu dữ liệu bằng cách sử dụng ràng buộc mặc định có thể là một quá trình phức tạp -- cách tiếp cận đúng không được ghi chép đầy đủ. Đây cũng là một tình huống mà mã bị lỗi âm thầm. Các nhà phát triển sẽ thấy các trang web của họ được điền dữ liệu chính xác, nhưng các giá trị sẽ không hiển thị trong mô hình khi nó đến hành động của bộ điều khiển cho HttpPost

Để hiểu rõ hơn về điều này, trước tiên chúng ta sẽ xem xét vấn đề, sau đó trình bày cách viết mã chức năng một cách chính xác

Những gì không làm

Trong mã Razor cho danh sách khách hàng ở trên, chúng tôi thấy rằng chúng tôi có thể điền danh sách bằng cách sử dụng vòng lặp

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
26 và
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
55
HtmlHelper method. If we used a ______126 cho danh sách đơn đặt hàng, thì
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
57
.

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
0

html

Điều đó thật hay và ngắn gọn, và dường như tận dụng sức mạnh của các phương thức mở rộng Razor HtmlHelper để tạo HTML "tự động". Vấn đề là;

HTML được tạo bởi đoạn mã trước sẽ trông như thế này

Javascript đặt thuộc tính ViewModel

Các phần tử biểu mẫu có tên và ID phần tử mơ hồ

Hãy nhìn vào các khu vực được đánh dấu màu vàng. Mỗi hàng là một hộp văn bản riêng biệt trên biểu mẫu được hiển thị ở trên cho chế độ xem Đơn hàng/Chỉ mục. Mỗi bản ghi có cùng giá trị cho các phần tử ____258

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
59 . ____310 . Không có cách nào để xác định các bản ghi một cách rõ ràng, MVC bỏ cuộc và trả về
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
11
cho
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
48
field of the
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
50
to which the Order/Index view is bound.

Dữ liệu thu thập ràng buộc chính xác

Trong mã Razor cho danh sách đơn hàng của một khách hàng cụ thể, chúng tôi đã sử dụng vòng lặp

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
45 với giá trị chỉ mục biến cục bộ
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
15
. The loop looks like this:

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1

csharp

Lưu ý các chi tiết sau

  1. Giá trị chỉ mục

    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    15 xuất hiện trong biểu thức lambda cho từng thành phần biểu mẫu được tạo bởi vòng lặp.

    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    17

  2. Trình trợ giúp ____318 Html được sử dụng, thay vì trình trợ giúp chung chung hơn (và tự động hóa)

    1using System;
    2using System.Collections.Generic;
    3using System.ComponentModel.DataAnnotations;
    4using System.ComponentModel.DataAnnotations.Schema;
    5using Blip.Entities.Geographies;
    6using Blip.Entities.Orders;
    7
    8namespace Blip.Entities.Customers
    9{
    10    public class Customer
    11    {
    12        public Customer()
    13        {
    14            Orders = new HashSet<Order>();
    15        }
    16
    17        [Key]
    18        [Column(Order = 0)]
    19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
    20        public Guid CustomerID { get; set; }
    21
    22        [Required]
    23        [MaxLength(128)]
    24        public string CustomerName { get; set; }
    25
    26        [Required]
    27        [MaxLength(3)]
    28        public string CountryIso3 { get; set; }
    29
    30        [MaxLength(3)]
    31        public string RegionCode { get; set; }
    32
    33        public virtual Country Country { get; set; }
    34
    35        public virtual Region Region { get; set; }
    36
    37        public virtual ICollection<Order> Orders { get; set; }
    38    }
    39}
    55 and
    1using System;
    2using System.Collections.Generic;
    3using System.ComponentModel.DataAnnotations;
    4using System.ComponentModel.DataAnnotations.Schema;
    5using Blip.Entities.Geographies;
    6using Blip.Entities.Orders;
    7
    8namespace Blip.Entities.Customers
    9{
    10    public class Customer
    11    {
    12        public Customer()
    13        {
    14            Orders = new HashSet<Order>();
    15        }
    16
    17        [Key]
    18        [Column(Order = 0)]
    19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
    20        public Guid CustomerID { get; set; }
    21
    22        [Required]
    23        [MaxLength(128)]
    24        public string CustomerName { get; set; }
    25
    26        [Required]
    27        [MaxLength(3)]
    28        public string CountryIso3 { get; set; }
    29
    30        [MaxLength(3)]
    31        public string RegionCode { get; set; }
    32
    33        public virtual Country Country { get; set; }
    34
    35        public virtual Region Region { get; set; }
    36
    37        public virtual ICollection<Order> Orders { get; set; }
    38    }
    39}
    40
    .

  3. Các trường

    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    51 và
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    52
    được đặt thành < .
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    53
    using the HTML
    1@foreach(var item in Model)
    2{
    3    <tr>
    4        <td>
    5            @Html.DisplayFor(modelItem => item.CustomerID)
    6        </td>
    7        <td>
    8            @Html.DisplayFor(modelItem => item.CustomerName)
    9        </td>
    10        <td>
    11            @Html.DisplayFor(modelItem => item.CountryName)
    12        </td>
    13        <td>
    14            @Html.DisplayFor(modelItem => item.RegionName)
    15        </td>
    16        <td>
    17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
    18        </td>
    19    </tr>
    20}
    54
    attribute, rather than using
    1using System;
    2using System.Collections.Generic;
    3using System.ComponentModel.DataAnnotations;
    4using System.ComponentModel.DataAnnotations.Schema;
    5using Blip.Entities.Geographies;
    6using Blip.Entities.Orders;
    7
    8namespace Blip.Entities.Customers
    9{
    10    public class Customer
    11    {
    12        public Customer()
    13        {
    14            Orders = new HashSet<Order>();
    15        }
    16
    17        [Key]
    18        [Column(Order = 0)]
    19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
    20        public Guid CustomerID { get; set; }
    21
    22        [Required]
    23        [MaxLength(128)]
    24        public string CustomerName { get; set; }
    25
    26        [Required]
    27        [MaxLength(3)]
    28        public string CountryIso3 { get; set; }
    29
    30        [MaxLength(3)]
    31        public string RegionCode { get; set; }
    32
    33        public virtual Country Country { get; set; }
    34
    35        public virtual Region Region { get; set; }
    36
    37        public virtual ICollection<Order> Orders { get; set; }
    38    }
    39}
    55
    .

  4. Kiểu dáng hộp văn bản Bootstrap được áp dụng bằng cách thêm thuộc tính ______356 .

HTML được tạo trông như thế này

Javascript đặt thuộc tính ViewModel

Các phần tử biểu mẫu có thuộc tính tên và ID riêng biệt

Khi các khu vực được đánh dấu màu vàng hiển thị, mỗi trường có

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
58 và
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
59
attribute that is distinct for each record. The record index gives MVC something to use to bind the form data to the data model.

Bằng cách đặt điểm dừng trong hành động của bộ điều khiển HttpPost cho chế độ xem Đơn hàng/Chỉ mục, chúng tôi có thể thấy rằng dữ liệu mới mà chúng tôi đã nhập, "xúc tiến" trong

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
59 field of record 0, is being posted back to the server along with the values for the
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
53
fields.

Javascript đặt thuộc tính ViewModel

Gỡ lỗi Visual Studio Hiển thị các giá trị của Trình kiểm tra thuộc tính cho một

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
47 Thực thể

Đăng dữ liệu hiển thị

Như chúng tôi đã lưu ý ở trên, chúng tôi đang sử dụng

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
18 Trình trợ giúp Html cho các trường chỉ đọc, thay vì
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
55
helper. This is so we can return the data to the controller when the HttpPost event fires (when the Save button on the page is pressed).

Khi MVC tạo HTML cho

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
55 trình trợ giúp HTML, nó sẽ hiển thị phần tử dưới dạng văn bản đơn giản.

Ví dụ: một ô của bảng được mã hóa như thế này

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
2

html

sẽ kết xuất như thế này

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
3

html

Đó chỉ là văn bản thẳng, vì vậy không có cách nào để MVC liên kết nó với mô hình

Nhưng một ô của bảng dành cho trường văn bản chỉ hiển thị được mã hóa như thế này

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
4

html

sẽ kết xuất như thế này

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
5

html

Là trường

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
95 , phần tử này sẽ được chuyển lại cho bộ điều khiển trong thời gian
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
96< . Bởi vì nó có một
. Because it has a distinct ______259 riêng biệt, nên dữ liệu trong trường
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
53
này có thể . Các giá trị cho ______120
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
51
cung cấp cho chúng tôi các giá trị chỉ mục cần thiết để lưu .
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
59
.

Cũng xin lưu ý rằng trong khi chúng tôi đang hiển thị

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
52 dưới dạng
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
53
text field, and thereby returning it during the
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
96
event, we don't have to. Only the index values necessary to save the changed data need to be returned in the
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
96
.

Trong ví dụ của chúng tôi, trường ____351 được hiển thị nhưng

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
20 field is included in the form using the
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
18
HtmlHelper. As coded, it looks like this:

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
6

html

Ngoài việc bị ẩn trên HTML được cung cấp cho ứng dụng khách, đây là phần tử dữ liệu đầy đủ tính năng được liên kết với mô hình chế độ xem mà

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
9 controller action for the Index view. Using ______418 là một cách thuận tiện để giữ cho bố cục biểu mẫu đơn giản trong khi bao gồm tất cả dữ liệu cần thiết để xác định các bản ghi sẽ được cập nhật trong
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
96
.

Người đọc thông thái có thể nhận ra rằng trong trường hợp nghiên cứu điển hình của chúng tôi, trường _______120 là không cần thiết để lưu thay đổi vào

1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
1 object. Because
1@foreach(var item in Model)
2{
3    <tr>
4        <td>
5            @Html.DisplayFor(modelItem => item.CustomerID)
6        </td>
7        <td>
8            @Html.DisplayFor(modelItem => item.CustomerName)
9        </td>
10        <td>
11            @Html.DisplayFor(modelItem => item.CountryName)
12        </td>
13        <td>
14            @Html.DisplayFor(modelItem => item.RegionName)
15        </td>
16        <td>
17            @Html.ActionLink("Orders", "Index", "Order",  new { customerid = item.CustomerID }, null)
18        </td>
19    </tr>
20}
51
là một
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
05
nó vốn đã cung cấp một mã định danh duy nhất cho một cá nhân .

Hành động điều khiển HttpPost

Bằng cách sử dụng mẫu thiết kế kho lưu trữ, chúng tôi có thể giữ cho các hành động của bộ điều khiển của mình trở nên đơn giản. Trong trường hợp của hành động

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
9 cho chế độ xem Đơn hàng/Chỉ mục, tất cả những gì chúng ta cần làm là xác thực
1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Geographies;
6using Blip.Entities.Orders;
7
8namespace Blip.Entities.Customers
9{
10    public class Customer
11    {
12        public Customer()
13        {
14            Orders = new HashSet<Order>();
15        }
16
17        [Key]
18        [Column(Order = 0)]
19        [DatabaseGenerated(DatabaseGeneratedOption.None)]
20        public Guid CustomerID { get; set; }
21
22        [Required]
23        [MaxLength(128)]
24        public string CustomerName { get; set; }
25
26        [Required]
27        [MaxLength(3)]
28        public string CountryIso3 { get; set; }
29
30        [MaxLength(3)]
31        public string RegionCode { get; set; }
32
33        public virtual Country Country { get; set; }
34
35        public virtual Region Region { get; set; }
36
37        public virtual ICollection<Order> Orders { get; set; }
38    }
39}
50< .
and pass it to the appropriate repository method.

đốm sáng. Web\Controllers\OrdersController. cs

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
7

csharp

Phương pháp lưu kho lưu trữ

Phương thức lưu trữ được liên kết với chế độ xem danh sách đơn đặt hàng của chúng tôi cũng có thể đơn giản. Tất cả những gì chúng ta cần làm là tìm các bản ghi phù hợp trong bảng Đơn hàng và cập nhật chúng

đốm sáng. Data\Orders\OrdersRepository. cs

1using System;
2using System.Collections.Generic;
3using System.ComponentModel.DataAnnotations;
4using System.ComponentModel.DataAnnotations.Schema;
5using Blip.Entities.Customers;
6using Blip.Entities.Items;
7
8namespace Blip.Entities.Orders
9{
10    public class Order
11    {
12        public Order()
13        {
14            Items = new HashSet<Item>();
15        }
16
17        [Key]
18        [DatabaseGenerated(DatabaseGeneratedOption.None)]
19        public Guid OrderID { get; set; }
20
21        [Required]
22        public Guid CustomerID { get; set; }
23
24        [Required]
25        public DateTime OrderDate { get; set; }
26
27        [Required]
28        [MaxLength(128)]
29        public string Description { get; set; }
30
31        public virtual ICollection<Item> Items { get; set; }
32
33        public virtual Customer Customer { get; set; }
34    }
35}
8

csharp

ThêmIthông tin

Nếu bạn muốn tìm hiểu sâu hơn về các chủ đề được thảo luận trong hướng dẫn này, thì đây là danh sách tài nguyên được tuyển chọn

Các lớp đào tạo PluralSight liên quan

PluralSight cung cấp một số khóa học về các chủ đề được đề cập trong hướng dẫn này. Sau đây là một số gợi ý được tổ chức bởi công nghệ

(Các) khóa học công nghệASP. NET, EF, Xem mô hìnhThực tiễn tốt nhất trong ASP. BỌC LƯỚI. Thực thể, Xác thực và Xem Mô hìnhASP. Ứng dụng NETBuilding với ASP. NET MVC 4C#C# Nguyên tắc cơ bản với Visual Studio 2015Entity FrameworkBắt đầu với Entity Framework 6MVCASP. NET MVC 5 Nguyên tắc cơ bản

Mã nghiên cứu điển hình trên GitHub

Các giải pháp Visual Studio hoàn chỉnh được mô tả trong hướng dẫn này có sẵn trên GitHub

ProjectRepoBlipBindinghttps. //github. com/ajsaulsberry/BlipBinding

Bạn có thể rẽ nhánh các dự án, chạy mã và tự mình thử nghiệm

Lưu ý rằng dự án mẫu không nhằm mục đích nghiên cứu trường hợp thực tế hoặc mã sản xuất;

Các nguồn lực khác

từ chối trách nhiệm. Pluralsight và tác giả của Hướng dẫn này không chịu trách nhiệm về nội dung, tính chính xác hoặc tính khả dụng của tài nguyên bên thứ 3