Hướng dẫn how to read from razor view to client side javascript? - cách đọc từ chế độ xem dao cạo sang javascript phía máy khách?

Giới thiệu

Các phương thức và tiện ích mở rộng AJAX trong System.web.mvc và System.web.mvc.ajax không gian tên tên tên gọi với các chế độ xem một phần JavaScript và MVC để tạo các trang web tương tác linh hoạt với mã tối thiểu. Khi sử dụng các tài nguyên này, các nhà phát triển nên nhận thức được một vài kỹ thuật cần thiết để tạo mã hiệu quả.

Các mã trong hướng dẫn này có nguồn gốc từ cùng một giải pháp Visual Studio được sử dụng cho hướng dẫn đồng hành, có sẵn trên GitHub. Bạn có thể tải xuống và chạy dự án để xem các kỹ thuật được minh họa trong hướng dẫn này trong hành động và tự mình thử nghiệm.

Một số chi tiết

Nghiên cứu trường hợp là một giải pháp Visual Studio 2017 đa dự án được phát triển từ mẫu MVC ASP.NET .NET Framework mặc định. Nó sử dụng Framework 6.1 và các mẫu thiết kế của kho lưu trữ và mô hình chế độ xem [MVVM].

Giải pháp bao gồm ba dự án tạo thành các lớp khác nhau của ứng dụng:

Dự ánLớp ứng dụng
Blip.dataBối cảnh dữ liệu và kho lưu trữ
BLIP.EntitiesThực thể dữ liệu
BLIP.WEBGiao diện người dùng [lượt xem] và logic doanh nghiệp [Bộ điều khiển]

Ứng dụng nghiên cứu trường hợp, Blipajax là một hệ thống đơn giản để thu thập, lưu trữ và truy xuất địa lý và thông tin khác về khách hàng. Nó không sẵn sàng sản xuất từ ​​các quan điểm thiết kế hoặc mã hóa; Nó tồn tại để minh họa các khái niệm được thảo luận trong hướng dẫn này. Để biết mô tả đầy đủ về giải pháp ví dụ, xem Hướng dẫn đồng hành.BlipAjax is a simple system for gathering, storing, and retrieving geographic and other information about customers. It's not production-ready from either the design or coding perspectives; it exists to illustrate the concepts discussed in this guide. For a complete description of the example solution, see the companion guide.

Tóm lại, trang khách hàng/chỉnh sửa bao gồm chế độ xem cha mẹ và ba chế độ xem một phần. Chế độ xem phần thứ ba có thể là một chế độ xem một phần để tạo địa chỉ e-mail mới hoặc chế độ xem một phần để tạo địa chỉ bưu chính mới, tùy thuộc vào giá trị được chọn trong chế độ xem một phần thứ hai. Minh họa sau đây cho thấy khách hàng/chỉnh sửa với các chú thích xác định chế độ xem [một phần], mô hình xem và hành động của bộ điều khiển [phương thức] liên quan đến việc hiển thị chế độ xem.Customer/Edit page consists of a parent view and three partial views. The third partial view can be either a partial view to create a new e-mail address or a partial view to create a new postal address, depending on the value selected in the second partial view. The following illustration shows Customer/Edit with annotations identifying the [partial] view, view model, and the controller action [method] associated with rendering the view.

Trên trang Khách hàng/Chỉnh sửa, các hàm JavaScript được sử dụng để điền vào danh sách thả xuống trạng thái/khu vực với các giá trị tương ứng với giá trị được chọn trong thả xuống quốc gia. Khi người dùng thay đổi quốc gia đã chọn, các giá trị cho trường tiểu bang/khu vực phải được đặt lại.Customer/Edit page, JavaScript functions are used to populate the State/Region dropdown list with the values that correspond with the value selected in the Country dropdown. As the user changes the selected country the values for the State/Region field must be reset.

Chế độ xem một phần của JavaScript và Ajax

Có hai khía cạnh để thực hiện JavaScript với các chế độ xem dao cạo một phần của Ajax:

  1. Thư viện JavaScript cung cấp chức năng AJAX cần thiết cho các phương thức trợ giúp ASP.NET AJAX

  2. Các tập lệnh dành riêng cho triển khai cung cấp chức năng phía máy khách

Trong giải pháp ví dụ, các tập lệnh dành riêng cho triển khai là các tập lệnh có giá trị thả xuống trạng thái/khu vực.State/Region dropdown values.

Thư viện JavaScript

Việc thực hiện thành công các chế độ xem một phần AJAX phụ thuộc vào việc tải đúng một số thư viện JavaScript.

JQuery và các thư viện toàn trang web khác

Mẫu MVC ASP.NET mặc định bao gồm tệp _layout.cshtml tiêu chuẩn thực hiện các tính năng của Navigator và bao gồm các tham chiếu đến các thư viện JavaScript tiêu chuẩn. Bởi vì _layout.cshtml được bao gồm trong mọi chế độ xem trong dự án web, nên đây là một nơi tốt để bao gồm các tài liệu tham khảo tập lệnh triển khai chức năng cho các yếu tố trang, chẳng hạn như navbar được tìm thấy trên mỗi trang.navbar found included on every page.

Hầu hết các thư viện JavaScript được hiển thị ở dưới cùng của phần tử

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
0 của _layout.cshtml bằng chức năng gói MVC. Gói duy nhất được tải trong phần tử
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
1 là phiên bản 2.8.3 của Thư viện Modernizr, phiên bản hiện đại của Modernizr được bao gồm cho mục đích phát triển. Không nên bao gồm tham chiếu hiện đại trong _layout.cshtml trong mã sản xuất. Bao gồm các tài liệu tham khảo thư viện tập lệnh và tập lệnh ở cuối trang cải thiện hiệu suất kết xuất trang.
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
0
element of _Layout.cshtml using MVC bundling functionality. The only bundle loaded in the
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
1
element is version 2.8.3 of the Modernizr library, a dated version of Modernizr included for development purposes. The Modernizr reference included in _Layout.cshtml should not be included in production code. Including script library references and scripts at the end of the page improves page rendering performance.

Chức năng giao diện người dùng được cung cấp bởi Khung Bootstrap CSS được bao gồm trong thư viện bootstrap.js được tải với gói bootstrap. Bootstrap.js phụ thuộc vào thư viện jQuery phổ biến, được tải bằng gói jQuery.Bootstrap CSS framework is included in the Bootstrap.js library loaded with the Bootstrap bundle. Bootstrap.js depends on the ubiquitous jQuery library, which is loaded using the jquery bundle.

Định nghĩa của gói jQuery cho phép phiên bản jQuery hiện tại được tải mà không thay đổi định nghĩa gói hoặc _layout.cshtml để khớp với số phiên bản hiện tại. Bằng cách này, thư viện jQuery có thể được cập nhật thông qua gói NUGET được liên kết mà không cập nhật mã để giữ số phiên bản đồng bộ.jquery bundle enables the current version of jQuery to be loaded without changing the bundle definition or _Layout.cshtml to match the current version number. This way, the jQuery library can be updated through the associated NuGet package without updating the code to keep the version number in sync.

Chức năng AJAX phụ thuộc vào thư viện jQuery, nhưng không phải thư viện bootstrap. Nếu dự án web không triển khai Khung Bootstrap CSS, thư viện Bootstrap không cần thiết. Trong giải pháp ví dụ, Blipajax, Khung Bootstrap CSS, được sử dụng để cung cấp kiểu dáng và chức năng giao diện người dùng cho một số yếu tố giao diện người dùng, bao gồm các yếu tố biểu mẫu như danh sách thả xuống quốc gia và trạng thái/khu vực.jQuery library, but not the Bootstrap library. If the web project does not implement the Bootstrap CSS framework, the Bootstrap library is unnecessary. In the example solution, BlipAjax, the Bootstrap CSS framework, is used to provide user interface styling and functionality for a number of user interface elements, including form elements like the Country and State/Region dropdown lists.

_Layout.cshtml

1DOCTYPE html>
2
3
4    
5    
6    @ViewBag.Title - My ASP.NET Application
7    @Styles.Render["~/Content/css"]
8    @Scripts.Render["~/bundles/modernizr"]
9    @RenderSection["header", required: false]
10
11
12    
13        
14            
15                
16                    
17                    
18                    
19                
20                @Html.ActionLink["Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }]
21            
22            
23                
24                    @Html.ActionLink["Home", "Index", "Home"]
25                    @Html.ActionLink["About", "About", "Home"]
26                    @Html.ActionLink["Contact", "Contact", "Home"]
27                
28            
29        
30    
31    
32        @RenderBody[]
33        
34        
35            @DateTime.Now.Year - My ASP.NET Application
36        
37    
38
39    @Scripts.Render["~/bundles/jquery"]
40    @Scripts.Render["~/bundles/bootstrap"]
41    @RenderSection["scripts", required: false]
42
43

HTML

Lưu ý rằng tệp _layout.cshtml cũng bao gồm chỉ thị dao cạo

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
2 cho phần
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
3. Điều này khiến cho bất kỳ tài liệu tham khảo và tập lệnh thư viện JavaScript bổ sung nào được tải sau gói jQuery. Thứ tự của các yếu tố này rất quan trọng để làm cho chức năng AJAX hoạt động và thứ tự tải là một lĩnh vực mà các nhà phát triển thường gặp khó khăn.
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
2
Razor directive for the
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
3
section. This causes any additional JavaScript library references and scripts to be loaded after the jQuery bundle. The order of these elements is important to getting Ajax functionality to work, and the load order is an area where developers have frequently experienced difficulty.

jQuery-validate.js, jQuery-validate-unobtrusies.js, và jQuery-unobtrious-ajax.js

Các trang bao gồm các yếu tố

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
4, chẳng hạn như khách hàng/chỉnh sửa, có thể triển khai xác thực dữ liệu phía khách hàng bằng các thư viện jQuery bổ sung và, tùy chọn, các tính năng của khung Bootstrap để tạo kiểu. Gói
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
5 tải cả hai thư viện cần thiết:
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
4
elements, such as Customer/Edit, can implement client-side data validation using additional jQuery libraries and, optionally, features of the Bootstrap framework for styling. The
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
5
bundle loads both of the required libraries:

  1. jquery-validate-unobtrusive.js

Các thư viện này được tải qua gói jQueryVal.jqueryval bundle.

Các trang bao gồm chức năng AJAX, chẳng hạn như khách hàng/chỉnh sửa cần bao gồm một thư viện JavaScript bổ sung:Customer/Edit need to include an additional JavaScript library:

jquery-unobtrusive-ajax.js

Thư viện này được tải qua gói jQueryUnobtruses.jqueryunobtrusive bundle.

Lưu ý rằng quy ước đặt tên tiêu chuẩn cho các thư viện tạo ra tiềm năng nhầm lẫn: gói jqueryunobtring không bao gồm thư viện cho AJAX không gây khó chịu và gói jqueryunobtrious không bao gồm thư viện để xác nhận không gây khó chịu.jqueryunobtrusive bundle does not include the library for unobtrusive Ajax and the jqueryunobtrusive bundle does not include the library for unobtrusive validation.

Như được hiển thị bên dưới, chế độ xem cha mẹ cho khách hàng/chỉnh sửa bao gồm chỉ thị

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
6 cho phần
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
7 được chỉ định trong _layout.cshtml. Các gói được ghi nhận ở trên được bao gồm trong phần này và, theo thứ tự của các chỉ thị trong _layout.cshtml, tải các thư viện được chỉ ra ở trên sau khi tải gói jQuery. Thứ tự này là điều cần thiết để thực hiện đúng thư viện Ajax JavaScript; Nó phụ thuộc vào jQuery.Customer/Edit includes a
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
6
directive for the
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
7
section specified in _Layout.cshtml. The bundles noted above are included in this section and, following the order of the directives in _Layout.cshtml, load the libraries indicated above after loading the jquery bundle. This order is essential to proper implementation of the Ajax JavaScript library; it depends on jQuery.

Thứ tự tải cũng rất quan trọng để thực hiện đúng các thư viện xác thực. Cả jQuery-validate.js và jQuery-validate-unobtrusius.js yêu cầu thư viện jQuery phải được tải trước.jquery-validate.js and jquery-validate-unobtrusive.js require the jQuery library to be loaded first.

Chế độ xem cha mẹ EDIT.CSHTML cũng bao gồm mã JavaScript dành riêng cho trang sẽ được mô tả chi tiết trong phần tiếp theo.Edit.cshtml parent view also includes page-specific JavaScript code which will be described in detail in the next section.

Customer/Edit.cshtml

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}

HTML

Lưu ý rằng

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
8 phía máy chủ được bao gồm trong các chỉ thị
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
9 cho các gói xác thực và AJAX để loại bỏ bất kỳ sự nhầm lẫn nào có thể phát sinh trên các tập lệnh nào được đưa vào các gói do kết quả của quy ước đặt tên tiêu chuẩn.
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
8
are included with the
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
9
directives for the validation and Ajax bundles to eliminate any confusion that might arise over which scripts are included in the bundles as a result of the standard naming convention.

Cũng lưu ý rằng thứ tự của các chỉ thị này là không bắt buộc, nhưng vì chức năng AJAX quan trọng hơn đối với hoạt động đúng của trang so với xác thực phía máy khách, nên nên tải thư viện đó ngay sau thư viện jQuery để giảm thiểu Khả năng các lỗi tải sẽ ngăn thư viện AJAX tải. Điều này đặc biệt quan trọng khi thêm sự phức tạp của việc tải các thư viện JavaScript từ các mạng phân phối nội dung [CDN] trong sản xuất với phiên bản cục bộ của thư viện như là một dự phòng khi CDN không có sẵn.

Cuối cùng, lưu ý rằng các yếu tố

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
4 của trang khách hàng/chỉnh sửa được bao gồm trong các chế độ xem một phần, thay vì chế độ xem EDIT.CSHTML của cha mẹ. Tuy nhiên, quy ước dành cho JavaScript được liên kết với chế độ xem một phần được đặt trong tệp .cshtml cho chế độ xem cha mẹ, thay vì chế độ xem một phần mà họ áp dụng. Phần tiếp theo sẽ hiển thị quy ước này trong thực tế cũng như mô tả một ngoại lệ đáng chú ý.
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
4
elements of the Customer/Edit page are included in the partial views, rather than the parent Edit.cshtml view. Nevertheless, the convention is for JavaScript associated with a partial view to be located in the .cshtml file for the parent view, rather than the partial view to which they apply. The next section will show this convention in practice as well as describing a notable exception.

Các kịch bản cụ thể thực hiện

Trang khách hàng/chỉnh sửa bao gồm các chức năng JavaScript dành riêng cho triển khai, trang web các giá trị cho danh sách thả xuống quốc gia và tiểu bang/khu vực trong cả hai chi tiết khách hàng chỉnh sửa và thêm một phần địa chỉ bưu chính mới của trang. Như đã lưu ý ở trên, quy ước mã hóa trong ASP.NET MVC là đặt các tập lệnh cho một chế độ xem một phần trong tệp .cshtml cho chế độ xem cha mẹ [đây là một ngoại lệ quan trọng đối với quy ước này, sẽ được mô tả chi tiết bên dưới].Customer/Edit page includes implementation-specific JavaScript functions that populate the values for the Country and State/Region dropdown lists in both the Edit customer details and Add a new postal address sections of the page. As noted above, the coding convention in ASP.NET MVC is to put the scripts for a partial view in the .cshtml file for the parent view [here is one important exception to this convention, which will be described in detail below].

Thực hiện khách hàngDitPartial.cshtmlCustomerEditPartial.cshtml

Vì các yếu tố kiểm soát liên quan đến các tập lệnh được bao gồm trong chế độ xem một phần của customereditpartial.cshtml, sẽ rất hữu ích khi xem xét chế độ xem một phần khi kiểm tra chức năng của các tập lệnh.CustomerEditPartial.cshtml, it will be helpful to look at the partial view when examining the functionality of the scripts.

CustomerEditPartial.cshtml

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}

HTML

Lưu ý các phương pháp trợ giúp

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
1 cho các phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
2 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
3.
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
1
helper methods for the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
2
and
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
3
elements.

Ngoài ra, lưu ý mã dao cạo sau:

1@{
2    Layout = null;
3}

HTML

Vì khách hàng đã được tải trong chế độ xem cha mẹ chỉnh sửa. Không có chỉ thị này, động cơ dao cạo bị nhầm lẫn và hiển thị các yếu tố từ tệp bố cục, chẳng hạn như chân trang.CustomerEditPartial.cshtml is the first partial view loaded in the Edit.cshtml parent view and because Edit.cshtml does not have an

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
4 directive, it is necessary to tell the Razor engine not to use _Layout.cshtml for the partial view. Without this directive, the Razor engine gets confused and renders elements from the layout file, such as the footer.

Tập lệnh cho khách hàngDitPartial.cshtmlCustomerEditPartial.cshtml

Trường hợp sử dụng cho biểu mẫu để chỉnh sửa quốc gia và tiểu bang/khu vực được liên kết với khách hàng có phần khác so với việc thêm một địa chỉ bưu chính mới, vì vậy chức năng tập lệnh cũng có phần khác nhau. Khi khách hàng/trang chỉnh sửa được sử dụng, khách hàng đã tồn tại, do đó, có một hồ sơ hiện có bao gồm các giá trị cho quốc gia và tiểu bang/khu vực của khách hàng. Theo đó, tập lệnh cần đưa ra danh sách các giá trị cho trường Trạng thái/Vùng với các giá trị liên quan đến giá trị hiện có của quốc gia và đặt giá trị của điều khiển khu vực phù hợp, như sau trong các hàm đầu tiên của JavaScript trong Khách hàng/Chỉnh sửa. CSHTML, hiển thị dưới đây.Customer/Edit page is used, the customer already exists, so there is an existing record including values for the customer's Country and State/Region. Accordingly, the script needs to populate the list of values for the State/Region field with values associated with the existing value for Country and set the value of the Region control accordingly, as follows in the first of the JavaScript functions in Customer/Edit.cshtml, shown below.

Lưu ý rằng JavaScript dành riêng cho triển khai sử dụng thư viện jQuery ["

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
5"].
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
5
"].

Ellipsis ["

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
6"] chỉ ra những nơi mà mã không liên quan đã được điều chỉnh lại cho sự ngắn gọn.
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
6
"] indicates places where the irrelevant code has been redacted for brevity.

1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12

HTML

Hàm này nhận được giá trị của các phần tử

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 được cung cấp bởi mô hình chế độ xem customerItViewModel.cs, gọi hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 để thêm các giá trị phù hợp vào phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 và đặt lại giá trị của phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8.
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7
and
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
elements provided by the view model CustomerEditViewModel.cs, calls the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9
function to add the appropriate values to the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
element, and resets the value of the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
element.

Hàm

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 gọi phương thức
1@{
2    Layout = null;
3}
3 để có được danh sách các vùng cho quốc gia được xác định trong giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8. Sau đó, nó nối thêm các cặp giá trị khóa này vào danh sách các giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8, thêm một bản ghi trống trước khi thực hiện để trình bày thả xuống ở trạng thái không được chọn. Như đã lưu ý ở trên, hàm
1@{
2    Layout = null;
3}
6 cho EDIT.CSHTML đặt lại giá trị đã chọn cho
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 Sau khi danh sách được tải.
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9
function calls the
1@{
2    Layout = null;
3}
3
method to get a list of the regions for the country identified in the value for the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
element. It then appends these key-value pairs to the list of values for the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
element, adding a blank record before doing so to present the drop-down in an unselected state. As noted above, the
1@{
2    Layout = null;
3}
6
function for Edit.cshtml resets the selected value for
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
once the list is loaded.

1
2...
3function AddRegions[selectedCountry, regionsSelect] {
4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
5        if [regions != null && !jQuery.isEmptyObject[regions]] {
6            regionsSelect.append[$['', {
7                value: null,
8                text: ""
9            }]];
10            $.each[regions, function [index, region] {
11                regionsSelect.append[$['', {
12                    value: region.Value,
13                    text: region.Text
14                }]];
15            }];
16        };
17    }];
18

HTML

Hàm này nhận được giá trị của các phần tử

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 được cung cấp bởi mô hình chế độ xem customerItViewModel.cs, gọi hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 để thêm các giá trị phù hợp vào phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 và đặt lại giá trị của phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8.
1@{
2    Layout = null;
3}
8
controller action, see the BlipAjax example solution on GitHub.

Hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 gọi phương thức
1@{
2    Layout = null;
3}
3 để có được danh sách các vùng cho quốc gia được xác định trong giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8. Sau đó, nó nối thêm các cặp giá trị khóa này vào danh sách các giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8, thêm một bản ghi trống trước khi thực hiện để trình bày thả xuống ở trạng thái không được chọn. Như đã lưu ý ở trên, hàm
1@{
2    Layout = null;
3}
6 cho EDIT.CSHTML đặt lại giá trị đã chọn cho
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 Sau khi danh sách được tải.

Để biết thêm thông tin về hành động của bộ điều khiển

1@{
2    Layout = null;
3}
8, hãy xem giải pháp ví dụ Blipajax trên GitHub..cshtml. JavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view. This happens when loading the partial view with a
1@{
2    Layout = null;
3}
9
helper method, as in this section from Edit.cshtml:

1...
2
3    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
4
5...

HTML

Hàm này nhận được giá trị của các phần tử

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 được cung cấp bởi mô hình chế độ xem customerItViewModel.cs, gọi hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 để thêm các giá trị phù hợp vào phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 và đặt lại giá trị của phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8.CustomerEditPartial.cshtml and AddressTypePartial.cshtml enable the "componentization" of the Customer/Edit.cshtml view. Both partial views are rendered at the same time as the parent view, and thus the
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7
and
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8
elements are available for binding when the
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
2
event occurs.

Hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 gọi phương thức
1@{
2    Layout = null;
3}
3 để có được danh sách các vùng cho quốc gia được xác định trong giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8. Sau đó, nó nối thêm các cặp giá trị khóa này vào danh sách các giá trị cho phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8, thêm một bản ghi trống trước khi thực hiện để trình bày thả xuống ở trạng thái không được chọn. Như đã lưu ý ở trên, hàm
1@{
2    Layout = null;
3}
6 cho EDIT.CSHTML đặt lại giá trị đã chọn cho
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 Sau khi danh sách được tải.CreatePostalAddressPartial.cshtml

Để biết thêm thông tin về hành động của bộ điều khiển

1@{
2    Layout = null;
3}
8, hãy xem giải pháp ví dụ Blipajax trên GitHub.CreatePostalAddressPartial.cshtml is initiated by the
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
3
event of the Ajax form created by the AddressTypePartial.cshtml partial view, defined as follows:

AddressTypePartial.cshtml

1@model Blip.Entities.Customers.ViewModels.AddressTypeViewModel
2
3@using [Ajax.BeginForm["AddressTypePartial", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "CreateAddress", InsertionMode = InsertionMode.Replace }]]
4{
5    @Html.AntiForgeryToken[]
6...
7}

Một lưu ý về liên kết tập lệnh cho các chế độ xem một phần

Như đã lưu ý ở trên, quy ước cho JavaScript liên quan đến các chế độ xem một phần của dao cạo là đưa chúng vào chế độ xem cha mẹ .cshtml. Các hàm JavaScript có thể được liên kết với các phần tử trên chế độ xem một phần; Chế độ xem một phần được hiển thị cùng lúc với chế độ xem cha mẹ. Điều này xảy ra khi tải chế độ xem một phần bằng phương thức trợ giúp

1@{
2    Layout = null;
3}
9, như trong phần này từ EDIT.cshtml:
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
4
helper method form, the controller action
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
5
kicks off the rendering of the Ajax partial view rather than an
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
6
action for the CreatePostalAddressPartial.cshtml partial view. The partial view replaces the empty
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
7
element of Edit.cshtml [for a more detailed explanation of using Ajax partial views, see the companion guide].

Các chế độ xem một phần của customerEditPartial.cshtml và địa chỉTypePartial.cshtml cho phép "thành phần" của chế độ xem khách hàng/edit.cshtml. Cả hai chế độ xem một phần được hiển thị cùng lúc với chế độ xem cha mẹ, và do đó các yếu tố

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 có sẵn để ràng buộc khi sự kiện
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
2 xảy ra.Edit.cshtml cannot be bound to elements on the postal address partial view.

Các tập lệnh cho createdepostaladdresspartial.cshtml

Kết xuất của chế độ xem một phần CreatEpostalAddressPartial.CSHTML được bắt đầu bởi sự kiện

1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
3 của mẫu Ajax được tạo bởi Chế độ xem một phần của UnderstypePepartial.cshtml, được định nghĩa như sau:
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
6
"].

CreatePostalAddressPartial.cshtml

1@model Blip.Entities.Customers.ViewModels.PostalAddressEditViewModel
2
3
4    $["#AddressCountry"].change[function [] {
5        var selectedCountry = $["#AddressCountry"].val[];
6        var regionsSelect = $["#AddressRegion"];
7        regionsSelect.empty[];
8        if [selectedCountry != null && selectedCountry != ''] {
9            AddRegions[selectedCountry, regionsSelect];
10        }
11    }];
12
13
14@using [Html.BeginForm["CreatePostalAddressPartial", "Customer", FormMethod.Post]]
15{
16    @Html.AntiForgeryToken[]
17
18    
19        
20        Add a new postal address
21        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
22        @Html.HiddenFor[model => model.CustomerID]
23        
24            @Html.LabelFor[model => model.PostalAddressID, htmlAttributes: new { @class = "control-label col-md-2" }]
25            
26                @Html.EditorFor[model => model.PostalAddressID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
27            
28        
29 ...
30        
31            @Html.LabelFor[model => Model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => Model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id = "AddressCountry" }]
34                @Html.ValidationMessageFor[model => Model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37        
38            @Html.LabelFor[model => Model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
39            
40                @Html.DropDownListFor[model => Model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id = "AddressRegion" }]
41                @Html.ValidationMessageFor[model => Model.SelectedRegionCode, "", new { @class = "text-danger" }]
42            
43        
44
45        
46            
47                
48            
49        
50    
51}

HTML

Hàm này nhận được giá trị của các phần tử

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 được cung cấp bởi mô hình chế độ xem customerItViewModel.cs, gọi hàm
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
9 để thêm các giá trị phù hợp vào phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 và đặt lại giá trị của phần tử
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8.

  • Hàm

    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    9 gọi phương thức
    1@{
    2    Layout = null;
    3}
    3 để có được danh sách các vùng cho quốc gia được xác định trong giá trị cho phần tử
    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    8. Sau đó, nó nối thêm các cặp giá trị khóa này vào danh sách các giá trị cho phần tử
    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    8, thêm một bản ghi trống trước khi thực hiện để trình bày thả xuống ở trạng thái không được chọn. Như đã lưu ý ở trên, hàm
    1@{
    2    Layout = null;
    3}
    6 cho EDIT.CSHTML đặt lại giá trị đã chọn cho
    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    8 Sau khi danh sách được tải.
    1
    2    $[document].ready[function [] {
    3        var selectedCountry = $["#Country"].val[];
    4        var selectedRegion = $["#Region"].val[];
    5        var regionsSelect = $['#Region'];
    6        AddRegions[selectedCountry, regionsSelect];
    7        if [selectedRegion != null && selectedRegion != ''] {
    8            regionsSelect.val = selectedRegion;
    9        };
    10    }];
    11...
    12
    9
    element is not enclosed in a scripts section.

  • Để biết thêm thông tin về hành động của bộ điều khiển

    1@{
    2    Layout = null;
    3}
    8, hãy xem giải pháp ví dụ Blipajax trên GitHub.
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    0
    and
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    1
    are distinct from the names of the similar elements on the CustomerEditPartial.cshtml partial view. This is essential for proper binding: be sure elements have distinct
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    2
    and
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    3
    attributes, as appropriate.

  • Một lưu ý về liên kết tập lệnh cho các chế độ xem một phần

    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    1 drop-down is the
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    5
    event for the
    1
    2...
    3function AddRegions[selectedCountry, regionsSelect] {
    4    $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
    5        if [regions != null && !jQuery.isEmptyObject[regions]] {
    6            regionsSelect.append[$['', {
    7                value: null,
    8                text: ""
    9            }]];
    10            $.each[regions, function [index, region] {
    11                regionsSelect.append[$['', {
    12                    value: region.Value,
    13                    text: region.Text
    14                }]];
    15            }];
    16        };
    17    }];
    18
    0
    element.

  • Như đã lưu ý ở trên, quy ước cho JavaScript liên quan đến các chế độ xem một phần của dao cạo là đưa chúng vào chế độ xem cha mẹ .cshtml. Các hàm JavaScript có thể được liên kết với các phần tử trên chế độ xem một phần; Chế độ xem một phần được hiển thị cùng lúc với chế độ xem cha mẹ. Điều này xảy ra khi tải chế độ xem một phần bằng phương thức trợ giúp
    1@{
    2    Layout = null;
    3}
    9, như trong phần này từ EDIT.cshtml:Edit.cshtml parent view, including the jQuery library ["
    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    5
    "] and the function
    1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
    2
    3@{
    4    Layout = null;
    5}
    6
    7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
    8{
    9    @Html.AntiForgeryToken[]
    10
    11    
    12        
    13        Edit customer details
    14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
    15        
    16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
    17            
    18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
    19            
    20        
    21
    22        
    23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
    24            
    25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
    26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
    27            
    28        
    29
    30        
    31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
    32            
    33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
    34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
    35            
    36        
    37
    38        
    39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
    40            
    41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
    42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
    43            
    44        
    45
    46        
    47            
    48                
    49            
    50        
    51    
    52}
    9
    .

Các chế độ xem một phần của customerEditPartial.cshtml và địa chỉTypePartial.cshtml cho phép "thành phần" của chế độ xem khách hàng/edit.cshtml. Cả hai chế độ xem một phần được hiển thị cùng lúc với chế độ xem cha mẹ, và do đó các yếu tố

1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
7 và
1@model Blip.Entities.Customers.ViewModels.CustomerEditViewModel
2
3@{
4    Layout = null;
5}
6
7@using [Html.BeginForm["EditCustomerPartial", "Customer", FormMethod.Post]]
8{
9    @Html.AntiForgeryToken[]
10
11    
12        
13        Edit customer details
14        @Html.ValidationSummary[true, "", new { @class = "text-danger" }]
15        
16            @Html.LabelFor[model => model.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }]
17            
18                @Html.EditorFor[model => model.CustomerID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }]
19            
20        
21
22        
23            @Html.LabelFor[model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }]
24            
25                @Html.EditorFor[model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }]
26                @Html.ValidationMessageFor[model => model.CustomerName, "", new { @class = "text-danger" }]
27            
28        
29
30        
31            @Html.LabelFor[model => model.SelectedCountryIso3, htmlAttributes: new { @class = "control-label col-md-2" }]
32            
33                @Html.DropDownListFor[model => model.SelectedCountryIso3, new SelectList[Model.Countries, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Country" }]
34                @Html.ValidationMessageFor[model => model.SelectedCountryIso3, "", new { @class = "text-danger" }]
35            
36        
37
38        
39            @Html.LabelFor[model => model.SelectedRegionCode, htmlAttributes: new { @class = "control-label col-md-2" }]
40            
41                @Html.DropDownListFor[model => model.SelectedRegionCode, new SelectList[Model.Regions, "Value", "Text"], htmlAttributes: new { @class = "form-control", @id="Region" }]
42                @Html.ValidationMessageFor[model => model.SelectedRegionCode, "", new { @class = "text-danger" }]
43            
44        
45
46        
47            
48                
49            
50        
51    
52}
8 có sẵn để ràng buộc khi sự kiện
1
2    $[document].ready[function [] {
3        var selectedCountry = $["#Country"].val[];
4        var selectedRegion = $["#Region"].val[];
5        var regionsSelect = $['#Region'];
6        AddRegions[selectedCountry, regionsSelect];
7        if [selectedRegion != null && selectedRegion != ''] {
8            regionsSelect.val = selectedRegion;
9        };
10    }];
11...
12
2 xảy ra.

Ngoài ra, lưu ý rằng xác thực phía máy khách không phô trương được tải với gói

1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
5 trong chế độ xem cha mẹ sẽ hoạt động trên các phần tử biểu mẫu trong chế độ xem một phần được tải với AJAX.
1@*Models are in partial views.*@
2
3@section header {
4
5}
6
7@{
8    ViewBag.Title = "Edit Customer Information";
9}
10
11
12    
13        Edit Customer Information
14    
15
16
17
18    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
19
20
21
22    @Html.Action["AddressTypePartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
23
24
25
26
27
28    
29        
30            
31            
32                @Html.ActionLink["Back to List", "Index"]
33            
34        
35    
36
37
38@section Scripts {
39    @Scripts.Render["~/bundles/jqueryunobtrusive"] @*For unobtrusive-ajax*@
40    @Scripts.Render["~/bundles/jqueryval"] @*For validate and validate-unobtrusive*@
41
42    
43    $[document].ready[function [] {
44        var selectedCountry = $["#Country"].val[];
45        var selectedRegion = $["#Region"].val[];
46        var regionsSelect = $['#Region'];
47        AddRegions[selectedCountry, regionsSelect];
48        if [selectedRegion != null && selectedRegion != ''] {
49            regionsSelect.val = selectedRegion;
50        };
51    }];
52
53    $["#Country"].change[function [] {
54        var selectedCountry = $["#Country"].val[];
55        var regionsSelect = $['#Region'];
56        regionsSelect.empty[];
57        if [selectedCountry != null && selectedCountry != ''] {
58            AddRegions[selectedCountry, regionsSelect];
59        }
60    }];
61
62    function AddRegions[selectedCountry, regionsSelect] {
63        $.getJSON['@Url.Action["GetRegions"]', { iso3: selectedCountry }, function [regions] {
64            if [regions != null && !jQuery.isEmptyObject[regions]] {
65                regionsSelect.append[$['', {
66                    value: null,
67                    text: ""
68                }]];
69                $.each[regions, function [index, region] {
70                    regionsSelect.append[$['', {
71                        value: region.Value,
72                        text: region.Text
73                    }]];
74                }];
75            };
76        }];
77    }
78    
79}
5
bundle in the parent view will work on the form elements in the partial view loaded with Ajax.

Sự kết luận

Khi được cấu trúc đúng, mã JavaScript có thể mở rộng sức mạnh của các thư viện JavaScript và mã tùy chỉnh thành các chế độ xem một phần dao cạo được hiển thị với thư viện AJAX không gây khó chịu. Các bước chính là:

  • Tải jQuery trong _layout.cshtml

  • Tải jQuery-unobtrious-ajax.js, jQuery.validate.js, và jQuery.validate.unobtrusive.js trong phần

    1...
    2
    3    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
    4
    5...
    0 của các trang thích hợp
    1...
    2
    3    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
    4
    5...
    0
    section of appropriate pages

  • Bao gồm các tập lệnh cho các chế độ xem một phần được hiển thị với chế độ xem cha mẹ trong chế độ xem cha mẹ

  • Bao gồm các tập lệnh cho các chế độ xem một phần được hiển thị với AJAX trong chế độ xem một phần

  • Bao gồm các chức năng phổ biến trong phần
    1...
    2
    3    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
    4
    5...
    0 của chế độ xem cha mẹ
    1...
    2
    3    @Html.Action["EditCustomerPartial", new { id = Url.RequestContext.RouteData.Values["id"] }]
    4
    5...
    0
    section of the parent view

Với các kỹ thuật này, có thể tạo các trang web mạnh mẽ, tương tác, dựa trên dữ liệu với mã tối thiểu và không phải buộc dây trên chi phí phát triển của việc triển khai khung phía khách hàng.

Thêm thông tin

Nếu bạn muốn đi sâu hơn vào các chủ đề được thảo luận trong hướng dẫn này hoặc thử nghiệm mã được hiển thị ở trên, thì sau đây là danh sách các tài nguyên được chọn.

Ví dụ dự án

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

Dự án ví dụ được cung cấp như là, không có bất kỳ bảo hành nào được thể hiện hoặc ngụ ý. Cả Pluralsight và tác giả đều không chịu trách nhiệm cho bất kỳ lỗi hoặc thiếu sót nào.

Các khóa học Pluralsight liên quan

ASP.NET MVC Các chủ đề nâng cao của Scott Allen, ngày 22 tháng 7 năm 2009. Mô -đun đầu tiên của khóa học này, AJAX với ASP.NET MVC cung cấp một cái nhìn tổng quan được tường thuật hay về công nghệ liên quan.

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

learn.jquery.com - Đây là trang web chính thức cho tài liệu JQuery, được cung cấp bởi Quỹ JQuery.

Microsoft: Bundling và Minification, của Rick Anderson, ngày 23 tháng 8 năm 2012 - Đây là hướng dẫn cần thiết để thực hiện gói và thu nhỏ trong ASP.NET MVC. Nếu bạn đang viết cho các thiết bị di động-và bạn nên-Google sẽ giảm tốc độ hiệu suất trang của bạn trừ khi bạn sử dụng gói và thu nhỏ, làm tổn thương hiệu suất của bạn trong tìm kiếm.

Microsoft: System.Web.MVC không gian tên và Microsoft: System.web.mvc.ajax không gian tên - Đây là các tài liệu tham khảo tài liệu chính tắc cho lớp Ajaxhelper, AjaxExtensions [bao gồm phương thức bắt đầu] và các lớp AJAXOPTION. Lưu ý rằng nếu bạn đi tìm thông tin trên Docs.microsoft.com, nó sẽ khởi động bạn vào các URL này. Mặc dù trang này đề cập đến Visual Studio 2013, trên thực tế, tài liệu gần đây nhất.

Wikipedia: AJAX [lập trình] - Một bản tóm tắt tốt đẹp về kiến ​​trúc, lịch sử và triển khai AJAX. Nền tảng tốt cho người mới bắt đầu tìm kiếm một nền tảng cơ bản trong công nghệ.

Bài Viết Liên Quan

Chủ Đề