Using JavaScript With Ajax and Razor Partial Views - Pluralsight - Pluralsight
Using JavaScript With Ajax and Razor Partial Views - Pluralsight - Pluralsight
Introduction
Introduction
Ajax helper methods and extensions in the
471
Some Details
The case study is a multi-project Visual
Studio 2017 solution developed from the
default [Link] .NET Framework MVC
template. It uses Entity Framework 6.1 and
the repository and Model View ViewModel
(MVVM) design patterns.
JavaScript Libraries
_Layout.cshtml
html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8" />
5 <meta name="viewport" content="width=d
6 <title>@[Link] - My [Link] App
7 @[Link]("~/Content/css")
8 @[Link]("~/bundles/modernizr")
9 @RenderSection("header", required: fal
10 </head>
11 <body>
12 <div class="navbar navbar-inverse navb
13 <div class="container">
14 <div class="navbar-header">
15 <button type="button" clas
16 <span class="icon-bar"
17 <span class="icon-bar"
18 <span class="icon-bar"
19 </button>
20 @[Link]("Applicat
21 </div>
22 <div class="navbar-collapse co
23 <ul class="nav navbar-nav"
24 <li>@[Link]("
25 <li>@[Link]("
26 <li>@[Link]("
27 </ul>
28 </div>
29 </div>
30 </div>
We use cookies to make interactions 31
with <div class="container body-content">
our websites and services easy and 32 @RenderBody()
meaningful. For more information about the Disable cookies Accept cookies and close this message
33can
cookies we use or to find out how you <hr />
disable cookies, click here.
[Link] 7/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
34 <footer>
35 <p>@[Link] - My ASP
36 </footer>
37 </div>
38
39 @[Link]("~/bundles/jquery")
40 @[Link]("~/bundles/bootstrap")
41 @RenderSection("scripts", required: fa
42 </body>
43 </html>
[Link], jquery-validate-
[Link], and jquery-unobtrusive-
[Link]
[Link]
Customer/[Link]
html
1 @*Models are in partial views.*@
2
3 @section header {
4
5 }
6
7 @{
8 [Link] = "Edit Customer Informa
9 }
10
11 <div class="row">
12 <div class="col-md-12">
13 <h2>Edit Customer Information</h2>
14 </div>
15 </div>
16
17 <div id="EditCustomerPartial">
18 @[Link]("EditCustomerPartial", ne
19 </div>
20
21 <div id="SelectAddressTypePartial">
22 @[Link]("AddressTypePartial", new
23 </div>
24
25 <div id="CreateAddress"></div>
26
We use cookies to make interactions 27
with <div class="row">
our websites and services easy and 28 <div class="form-group">
meaningful. For more information about the Disable cookies Accept cookies and close this message
29
cookies we use or to find out how you can <div class="col-md-12">
disable cookies, click here. 30 <hr />
[Link] 10/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
31 <div>
32 @[Link]("Back to
33 </div>
34 </div>
35 </div>
36 </div>
37
38 @section Scripts {
39 @[Link]("~/bundles/jqueryunobt
40 @[Link]("~/bundles/jqueryval")
41
42 <script type="text/javascript">
43 $(document).ready(function () {
44 var selectedCountry = $("#Country"
45 var selectedRegion = $("#Region")
46 var regionsSelect = $('#Region');
47 AddRegions(selectedCountry, region
48 if (selectedRegion != null && sele
49 [Link] = selectedRe
50 };
51 });
52
53 $("#Country").change(function () {
54 var selectedCountry = $("#Country"
55 var regionsSelect = $('#Region');
56 [Link]();
57 if (selectedCountry != null && sel
58 AddRegions(selectedCountry, re
59 }
60 });
61
62 function AddRegions(selectedCountry, r
63 $.getJSON('@[Link]("GetRegions
64 if (regions != null && !jQuery
65 [Link]($('<o
66 value: null,
67 text: ""
68 }));
69 $.each(regions, function (
70 [Link]($
71 value: [Link]
72 text: [Link]
73 }));
We use cookies to make interactions 74
with });
our websites and services easy and
75 the Disable cookies };Accept cookies and close this message
meaningful. For more information about
cookies we use or to find out how you
76can });
disable cookies, click here.
[Link] 11/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
77 }
78 </script>
79 }
Implementation-specific
Scripts
The 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
[Link] 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).
Implementation of
[Link]
[Link]
html
1 @model [Link]
2
3 @{
4 Layout = null;
5 }
6
7 @using ([Link]("EditCustomerPartia
8 {
9 @[Link]()
10
11 <div class="form-horizontal">
12 <hr />
13 <h4>Edit customer details</h4>
14 @[Link](true, "",
15 <div class="form-group">
16 @[Link](model => model
17 <div class="col-md-10">
18 @[Link](model => m
19 </div>
20 </div>
21
22 <div class="form-group">
23 @[Link](model => model
24 <div class="col-md-10">
25 @[Link](model => m
26 @[Link]
27 </div>
28 </div>
29
30 <div class="form-group">
31 @[Link](model => model
32 <div class="col-md-10">
33 @[Link](mode
34 @[Link]
35 </div>
36 </div>
37
38 <div class="form-group">
39 @[Link](model => model
40 <div class="col-md-10">
We use cookies to make interactions 41
with @[Link](mode
our websites and services easy and
42 the Disable cookies Accept
meaningful. For more information about @[Link]
cookies and close this message
cookies we use or to find out how you
43can </div>
disable cookies, click here.
[Link] 14/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
44 </div>
45
46 <div class="form-group">
47 <div class="col-md-offset-2 co
48 <input type="submit" value
49 </div>
50 </div>
51 </div>
52 }
html
1 @{
2 Layout = null;
3 }
html
1 <script type="text/javascript">
2 $(document).ready(function () {
3 var selectedCountry = $("#Country"
4 var selectedRegion = $("#Region")
5 var regionsSelect = $('#Region');
6 AddRegions(selectedCountry, region
7 if (selectedRegion != null && sele
8 [Link] = selectedRe
9 };
10 });
11 ...
12 </script>
html
1 <script type="text/javascript">
2 ...
3 function AddRegions(selectedCountry, regio
4 $.getJSON('@[Link]("GetRegions")',
5 if (regions != null && ![Link]
6 [Link]($('<optio
7 value: null,
8 text: ""
9 }));
10 $.each(regions, function (inde
11 [Link]($('<o
We use cookies to make interactions with
our websites and services easy and 12 value: [Link],
meaningful. For more information about
13 the Disable cookies Accept text:
[Link]
and close this message
cookies we use or to find out how you can
disable cookies, click here. 14 }));
[Link] 17/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
15 });
16 };
17 });
18 </script>
html
1 ...
2 <div id="EditCustomerPartial">
3 @[Link]("EditCustomerPartial", new
4 </div>
5 ...
Scripts for
[Link]
[Link]
csharp
1 @model [Link].A
2
3 @using ([Link]("AddressTypePartial"
4 {
5 @[Link]()
6 ...
7 }
[Link]
html
1 @model [Link]
2
3 <script type="text/javascript">
4 $("#AddressCountry").change(function (
5 var selectedCountry = $("#AddressC
6 var regionsSelect = $("#AddressReg
7 [Link]();
We use cookies to make interactions with
8 if (selectedCountry != null && sel
our websites and services easy and
9 the Disable cookies AddRegions(selectedCountry,
meaningful. For more information about re
Accept cookies and close this message
cookies we use or to find out how you
10can }
disable cookies, click here.
[Link] 20/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
11 });
12 </script>
13
14 @using ([Link]("CreatePostalAddres
15 {
16 @[Link]()
17
18 <div class="form-horizontal">
19 <hr />
20 <h4>Add a new postal address</h4>
21 @[Link](true, "",
22 @[Link](model => [Link]
23 <div class="form-group">
24 @[Link](model => model
25 <div class="col-md-10">
26 @[Link](model => m
27 </div>
28 </div>
29 ...
30 <div class="form-group">
31 @[Link](model => Model
32 <div class="col-md-5">
33 @[Link](mode
34 @[Link]
35 </div>
36 </div>
37 <div class="form-group">
38 @[Link](model => Model
39 <div class="col-md-5">
40 @[Link](mode
41 @[Link]
42 </div>
43 </div>
44
45 <div class="form-group">
46 <div class="col-md-offset-2 co
47 <input type="submit" value
48 </div>
49 </div>
50 </div>
51 }
Conclusion
When structured properly, JavaScript
code can extend the power of JavaScript
libraries and custom code to Razor partial
views rendered with the unobtrusive Ajax
library. The key steps are:
Example Project
BlipAjax
Other Resources
471
(/offer/2020/33-off-q3)
Disable cookies