LyraLearn AI Learning Platform
Exams
← Module 3 Β· Razor, jQuery and Bootstrap
🎧 Listen

jQuery Patterns That Scale

Enterprise MVC front ends run on jQuery, and they will for years β€” Kendo UI for ASP.NET Core is built on it, unobtrusive validation depends on it, and a decade of working screens use it. The goal isn't "modern JavaScript"; it's jQuery that stays maintainable at line-of-business scale. A handful of patterns do most of that work.

Start after the DOM, delegate for dynamic content

All wiring goes inside a DOM-ready handler β€” $(function () { ... }) β€” so elements exist before you bind to them. Put page scripts in the layout's scripts section so jQuery itself has loaded first; a $ is not defined error means you lost that ordering.

The pattern that separates journeyman from beginner jQuery is event delegation. Direct binding β€” $('.delete-btn').on('click', ...) β€” attaches handlers to the elements present right now. The moment AJAX replaces that table, every handler is gone, and "the button works until you search" becomes your bug report. Delegated binding attaches one handler to a stable ancestor and filters by selector at event time:

$('#orders-table').on('click', '.delete-btn', function () {
    var id = $(this).data('order-id');
    ...
});

Rows can be replaced all day; the handler survives. Habits that pay off alongside it: pass server values via data-* attributes (data-order-id="@Model.Id") instead of concatenating C# into script, and never hardcode URLs in JS β€” emit them with @Url.Action("Delete", "Orders") so routing changes can't strand your front end.

Talking to controllers with AJAX

The staple round trip: jQuery calls an action, the action returns Json(...) or a PartialView(...), jQuery updates the page.

$.post('@Url.Action("Search", "Orders")', form.serialize(), function (html) {
    $('#results').html(html);
});

Returning a partial view and injecting the HTML is usually simpler than returning JSON and rebuilding markup client-side β€” the server already knows how to render an order row. Reserve JSON for data consumed by logic (dropdown cascades, validation checks, Kendo data sources). Remember from Module 2 that JSON is camelCased on the wire β€” write response.customerName, not response.CustomerName. Always add a .fail() handler; an AJAX error with no handler is a silent no-op the user experiences as a dead button. And for POSTs, include the anti-forgery token β€” Lesson 4 shows the wiring.

Unobtrusive validation: the bridge

jQuery unobtrusive validation (jquery.validate + Microsoft's adapter) reads the data-val-* attributes that asp-for emitted from your data annotations and enforces the same rules client-side β€” no hand-written validation JS. Two operational facts: it only parses forms present at page load, so after injecting a form via AJAX you must call $.validator.unobtrusive.parse('#my-form'); and it is a convenience layer only β€” the server re-validates everything, as Lesson 4 drills end-to-end.

🧠 Quiz yourself on this lesson →

Ask the AI Tutor

Grounded in the course lessons β€” it cites its sources and says when it doesn't know.