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 ASP.NET .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>@ViewBag.Title - My ASP.NET App
7 @Styles.Render("~/Content/css")
8 @Scripts.Render("~/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 @Html.ActionLink("Applicat
21 </div>
22 <div class="navbar-collapse co
23 <ul class="nav navbar-nav"
24 <li>@Html.ActionLink("
25 <li>@Html.ActionLink("
26 <li>@Html.ActionLink("
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.
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 7/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
34 <footer>
35 <p>@DateTime.Now.Year - My ASP
36 </footer>
37 </div>
38
39 @Scripts.Render("~/bundles/jquery")
40 @Scripts.Render("~/bundles/bootstrap")
41 @RenderSection("scripts", required: fa
42 </body>
43 </html>
jquery-validate.js, jquery-validate-
unobtrusive.js, and jquery-unobtrusive-
ajax.js
jquery-unobtrusive-ajax.js
Customer/Edit.cshtml
html
1 @*Models are in partial views.*@
2
3 @section header {
4
5 }
6
7 @{
8 ViewBag.Title = "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 @Html.Action("EditCustomerPartial", ne
19 </div>
20
21 <div id="SelectAddressTypePartial">
22 @Html.Action("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 />
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 10/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
31 <div>
32 @Html.ActionLink("Back to
33 </div>
34 </div>
35 </div>
36 </div>
37
38 @section Scripts {
39 @Scripts.Render("~/bundles/jqueryunobt
40 @Scripts.Render("~/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 regionsSelect.val = selectedRe
50 };
51 });
52
53 $("#Country").change(function () {
54 var selectedCountry = $("#Country"
55 var regionsSelect = $('#Region');
56 regionsSelect.empty();
57 if (selectedCountry != null && sel
58 AddRegions(selectedCountry, re
59 }
60 });
61
62 function AddRegions(selectedCountry, r
63 $.getJSON('@Url.Action("GetRegions
64 if (regions != null && !jQuery
65 regionsSelect.append($('<o
66 value: null,
67 text: ""
68 }));
69 $.each(regions, function (
70 regionsSelect.append($
71 value: region.Valu
72 text: region.Text
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.
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 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
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).
Implementation of
CustomerEditPartial.cshtml
CustomerEditPartial.cshtml
html
1 @model Blip.Entities.Customers.ViewModels
2
3 @{
4 Layout = null;
5 }
6
7 @using (Html.BeginForm("EditCustomerPartia
8 {
9 @Html.AntiForgeryToken()
10
11 <div class="form-horizontal">
12 <hr />
13 <h4>Edit customer details</h4>
14 @Html.ValidationSummary(true, "",
15 <div class="form-group">
16 @Html.LabelFor(model => model
17 <div class="col-md-10">
18 @Html.EditorFor(model => m
19 </div>
20 </div>
21
22 <div class="form-group">
23 @Html.LabelFor(model => model
24 <div class="col-md-10">
25 @Html.EditorFor(model => m
26 @Html.ValidationMessageFor
27 </div>
28 </div>
29
30 <div class="form-group">
31 @Html.LabelFor(model => model
32 <div class="col-md-10">
33 @Html.DropDownListFor(mode
34 @Html.ValidationMessageFor
35 </div>
36 </div>
37
38 <div class="form-group">
39 @Html.LabelFor(model => model
40 <div class="col-md-10">
We use cookies to make interactions 41
with @Html.DropDownListFor(mode
our websites and services easy and
42 the Disable cookies Accept
meaningful. For more information about @Html.ValidationMessageFor
cookies and close this message
cookies we use or to find out how you
43can </div>
disable cookies, click here.
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 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 regionsSelect.val = selectedRe
9 };
10 });
11 ...
12 </script>
html
1 <script type="text/javascript">
2 ...
3 function AddRegions(selectedCountry, regio
4 $.getJSON('@Url.Action("GetRegions")',
5 if (regions != null && !jQuery.isE
6 regionsSelect.append($('<optio
7 value: null,
8 text: ""
9 }));
10 $.each(regions, function (inde
11 regionsSelect.append($('<o
We use cookies to make interactions with
our websites and services easy and 12 value: region.Value,
meaningful. For more information about
13 the Disable cookies Accept text:
cookiesregion.Text
and close this message
cookies we use or to find out how you can
disable cookies, click here. 14 }));
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 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 @Html.Action("EditCustomerPartial", new
4 </div>
5 ...
Scripts for
CreatePostalAddressPartial.cshtml
AddressTypePartial.cshtml
csharp
1 @model Blip.Entities.Customers.ViewModels.A
2
3 @using (Ajax.BeginForm("AddressTypePartial"
4 {
5 @Html.AntiForgeryToken()
6 ...
7 }
CreatePostalAddressPartial.cshtml
html
1 @model Blip.Entities.Customers.ViewModels
2
3 <script type="text/javascript">
4 $("#AddressCountry").change(function (
5 var selectedCountry = $("#AddressC
6 var regionsSelect = $("#AddressReg
7 regionsSelect.empty();
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.
https://www.pluralsight.com/guides/asp-net-mvc-using-javascript-with-ajax-and-razor-partial-views 20/26
8/19/2020 Using JavaScript with Ajax and Razor partial views | Pluralsight | Pluralsight
11 });
12 </script>
13
14 @using (Html.BeginForm("CreatePostalAddres
15 {
16 @Html.AntiForgeryToken()
17
18 <div class="form-horizontal">
19 <hr />
20 <h4>Add a new postal address</h4>
21 @Html.ValidationSummary(true, "",
22 @Html.HiddenFor(model => model.Cus
23 <div class="form-group">
24 @Html.LabelFor(model => model
25 <div class="col-md-10">
26 @Html.EditorFor(model => m
27 </div>
28 </div>
29 ...
30 <div class="form-group">
31 @Html.LabelFor(model => Model
32 <div class="col-md-5">
33 @Html.DropDownListFor(mode
34 @Html.ValidationMessageFor
35 </div>
36 </div>
37 <div class="form-group">
38 @Html.LabelFor(model => Model
39 <div class="col-md-5">
40 @Html.DropDownListFor(mode
41 @Html.ValidationMessageFor
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