AJAX in ASP.NET MVC AJAX, Partial Page Rendering, jQuery AJAX, MVC AJAX Helpers SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Telerik School Academy ASP.NET MVC.
Advertisements

JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
MVC Advanced & Reflection Reflection, Parsing Dynamic Data, Asynchronous Requests SoftUni Team Technical Trainers Software University
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
ASP.NET MVC Architecture Layouts, Filters, Sections, Helpers, Partial Views, Areas… SoftUni Team Technical Trainers Software University
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
HTML Forms Forms, Controls, Fields, Inputs, Submission, Validation SoftUni Team Technical Trainers Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Web Fundamentals (HTML and CSS)
ASP.NET SignalR SoftUni Team Technical Trainers Software University
JQuery Overview Unleash the Power of jQuery SoftUni Team Technical Trainers Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
SE-2840 Dr. Mark L. Hornick 1 Introduction to Ajax Asynchronous Javascript And XML.
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
AJAX with ASP.NET MVC Telerik Software Academy
CSS Transitions and Animations Animated HTML Elements SoftUni Team Technical Trainers Software University
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
DOM, jQuery, AJAX, REST Using Kinvey as JS Back-End
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
ASP.NET Integration Testing
Mocking tools for easier unit testing
MVC Architecture. Routing
Caching Data in ASP.NET MVC
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
ASP.NET MVC Introduction
Extending functionality using Collections
Presentation transcript:

AJAX in ASP.NET MVC AJAX, Partial Page Rendering, jQuery AJAX, MVC AJAX Helpers SoftUni Team Technical Trainers Software University

2 1.What is AJAX?  Raw AJAX vs. AJAX using a JS Library  Partial Page Rendering vs. JSON Service 2.AJAX with Unobtrusive JavaScript 3.AJAX MVC Helpers  ActionLink and BeginForm  Partial Views and AJAX 4.JSON, AJAX and ASP.NET MVC Table of Contents

What is AJAX? Asynchronous JavaScript and XML

4  AJAX is acronym of Asynchronous JavaScript and XML  AJAX == technique for asynchronously loading (in the background) of dynamic Web content and data from the Web server into a HTML page  Allows dynamically changing the DOM (client-side) in Web applications  Two styles of AJAX  Partial page rendering  Load an HTML fragment and display it in a  JSON service with client-side rendering  Loading a JSON object and render it at the client-side with JS / jQuery AJAX

5  AJAX advantages  Asynchronous calls  data is loaded after the page is shown  Minimal data transfer  less traffic, fast update  Responsiveness  better user experience  AJAX disadvantages  Requires more development efforts  The browser [Back] and [Refresh] buttons don't work  Might cause SEO problems: search engine bots may skip the AJAX AJAX: Pros and Cons

6  Browsers support XMLHttpRequest object  Exposes a JavaScript API to send raw AJAX requests  Send HTTP / HTTPS requests directly to the Web server  GET / POST / PUT / DELETE / other  AJAX response might be JSON, XML, HTML, as plain text, etc.  Same-origin policy   AJAX requests can only access the same server that served the original Web page executing the AJAX call The XMLHttpRequest Object

7 function updateServerTimeAjax() { var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest(); xhr.open("GET", "/Home/ServerTime", true); xhr.open("GET", "/Home/ServerTime", true); xhr.onreadystatechange = function() { xhr.onreadystatechange = function() { var timeDiv = document.getElementById("timeDisplay"); var timeDiv = document.getElementById("timeDisplay"); timeDiv.innerHTML = ''; timeDiv.innerHTML = ''; if (xhr.readyState == 4 && xhr.status == 200) { if (xhr.readyState == 4 && xhr.status == 200) { timeDiv.innerHTML = xhr.responseText; timeDiv.innerHTML = xhr.responseText; } } xhr.send(); xhr.send();} Raw AJAX – Example The request is finished and the response is ready HTTP status code is "200 OK"

8 AJAX with jQuery – Example  jQuery dramatically simplifies working with AJAX: Get Server Time Get Server Time <script> function updateServerTimeAjax () { function updateServerTimeAjax () { $("#timeDisplay").load("/Home/ServerTime"); $("#timeDisplay").load("/Home/ServerTime"); }</script>

AJAX with Unobtrusive JavaScript

 Unobtrusive JavaScript == no script is injected into page  Only data-attributes to configure the AJAX call settings  Requires Microsoft.jQuery.Unobtrusive.Ajax NuGet package  jquery.unobtrusive-ajax.js (AJAX helpers) AJAX with Unobtrusive JavaScript & jQuery <a data-ajax="true" href="/Home/ServerTime" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#timeDisplay"> data-ajax-update="#timeDisplay"> Load Server Time Load Server Time</a> 10

AJAX Helpers in ASP.NET

12  AJAX helpers add AJAX functionality to ASP.NET MVC  Two core features of AJAX helpers:  Invoke an action method asynchronously using AJAX  Use the Ajax.ActionLink() helper  Submit an entire form using AJAX  Use the Ajax.BeginForm() helper  AJAX helpers use AjaxOptions object with configurations AJAX Helpers in ASP.NET MVC

13  Url – URL to send request  HttpMethod – request method (GET / POST / PUT / DELETE / …)  InsertionMode – how to handle the received data  InsertAfter, InsertBefore or Replace  UpdateTargetId – HTML element to be changed  LoadingElementId – show / hide "Loading…" when loading  Events (JavaScript functions)  OnSuccess, OnFailure, OnBegin, OnComplete AjaxOptions Object

14  Defines an action link ( ) for loading data with AJAX Ajax.ActionLink Helper – Server Time", "ServerTime", null, new AjaxOptions { new AjaxOptions { HttpMethod = "GET", HttpMethod = "GET", UpdateTargetId = "timeDisplay", UpdateTargetId = "timeDisplay", LoadingElementId = "timeDisplayLoading", LoadingElementId = "timeDisplayLoading", InsertionMode = InsertionMode.Replace, InsertionMode = InsertionMode.Replace, OnBegin = "OnAjaxRequestBegin", OnBegin = "OnAjaxRequestBegin", OnFailure = "OnAjaxRequestFailure", OnFailure = "OnAjaxRequestFailure", OnSuccess = "OnAjaxRequestSuccess", OnSuccess = "OnAjaxRequestSuccess", OnComplete = "OnAjaxRequestComplete", OnComplete = "OnAjaxRequestComplete", }, new = "btn btn-primary" }) action controller

15  A form that submits AJAX request and renders a partial view Ajax.BeginForm Helper – (Ajax.BeginForm("Search", new AjaxOptions { new AjaxOptions { UpdateTargetId = "results", UpdateTargetId = "results", InsertionMode = InsertionMode.Replace InsertionMode = InsertionMode.Replace })) })){ Model)

16  Return a PartialView to the helpers (view without the layout) Ajax.BeginForm Helper – Example (2) public ActionResult Search(string query) { var result = BooksData var result = BooksData.GetAll().GetAll().AsQueryable().AsQueryable().Where(book => book.Title.Contains(query)).Where(book => book.Title.Contains(query)).Select(BookViewModel.FromBook).Select(BookViewModel.FromBook).ToList();.ToList(); return this.PartialView("_BookResult", result); return this.PartialView("_BookResult", result);}

JSON Services in ASP.NET MVC

18  MVC Ajax Helpers cover simple AJAX scenarios  Replacing HTML content  Partial page rendering  Other scenarios require some JavaScript  Auto-complete textboxes  Client-side validation  Invoking JSON services and render content at the client-side  Animations JSON Services in ASP.NET MVC

19  Return JsonResult in the action  Use jQuery.getJSON(…) MVC, JSON Results and JavaScript Rendering public JsonResult AllBooks() { var books = BooksData.GetAll(); var books = BooksData.GetAll(); return this.Json(books, JsonRequestBehavior.AllowGet); return this.Json(books, JsonRequestBehavior.AllowGet);} jQuery.getJSON("AllBooks", null, function(data) { jQuery(data).each(function (index, element) { jQuery(data).each(function (index, element) { var newBookElement = $(" +element.Title+" "); var newBookElement = $(" +element.Title+" "); $("#books").append(newBookElement); $("#books").append(newBookElement); }); });});

20  AJAX is powerful technique in Web development  Load data asynchronously  Without refreshing the entire Web page  AJAX Helpers in ASP.NET MVC simplify AJAX calls  Controllers return partial views, displayed on the Web page  Use ActionLink and BeginForm for simple AJAX calls  Still we can implement JSON service with client-side rendering  Typically use jQuery AJAX functionality Summary

? ? ? ? ? ? ? ? ? AJAX in ASP.NET MVC

SoftUni Diamond Partners

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 23  Attribution: this work may contain portions from  "ASP.NET MVC" course by Telerik Academy under CC-BY-NC-SA licenseASP.NET MVCCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg