1. Trang chủ
  2. » Công Nghệ Thông Tin

Pro ASP.NET MVC Framework phần 9 pdf

68 590 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 68
Dung lượng 16,29 MB

Nội dung

But sometimes, you might need something more powerful, because the Ajax.* helpers are limited in the following ways: • They only do simple page updates. They can inject a finished block of HTML into your existing page, but they have no support for retrieving or dealing with raw data (e.g., d ata in JSON format), and the only way of customizing how they manipulate your DOM is by explicitly returning a JavaScript statement from your action method. • When updating your DOM, they simply make elements appear or disappear. There’s no built-in support for making things fade or slide out, or performing any other fancy animation. • The programming model doesn’t naturally lend itself to retaining useful behavior when JavaScript is disabled. To overcome these limitations, you can write your own raw JavaScript (and deal with its compatibility issues manually) or make use of a full-fledged JavaScript library. For example, you could directly use Microsoft’s ASP.NET AJAX library. However, ASP.NET AJAX is a heavyweight option: its main selling point is its support for ASP.NET WebForms’ complicated server-side event and control model, but that’s not very interesting to ASP.NET MVC developers. With ASP.NET MVC, you’re free to use any Ajax or JavaScript library. The most popular option, judging by the overwhelming roar of approval coming from the world’s web developers, is to use jQuery. This option has become so popular that Microsoft now ships jQuery with ASP.NET MVC and has said they will include it in Visual Studio 2010, even though it isn’t a Microsoft product. So, what’s all the fuss about? Using jQuery with ASP.NET MVC Write less, do more: that’s the core promise of jQuery, a free, open source 5 JavaScript library first released in 2006. It’s won massive kudos from web developers on all platforms because it cuts the pain out of client-side coding. It provides an elegant CSS 3–based syntax for travers- ing your DOM, a fluent API for manipulating and animating DOM elements, and extremely concise wrappers for Ajax calls—all carefully abstracted to eliminate cross-browser differ- ences. 6 It’s easily extensible, has a rich ecosystem of free plug-ins, and encourages a coding style that retains basic functionality when JavaScript isn’t available. Sounds too good to be true? Well, I can’t really claim that it makes all client-side coding easy , but it is usually far easier than raw JavaScript, and it works great with ASP.NET MVC. Over the next few pages, you’ll learn the basic theory of jQuery and see it in action, adding some sparkle to typical ASP.NET MVC actions and views. Referencing jQuery E v er y new ASP.NET MVC project already has jQuery in its /Scripts folder . Like many other JavaScript libraries, it’s just a single .js file. To use it, you only need to reference it. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING 431 5. It’s available for commercial and personal use under both the MIT and GPL licenses. 6. Currently, it supports Firefox 2.0+, Internet Explorer 6+, Safari 3+, Opera 9+, and Chrome 1+. 10078ch12.qxd 4/9/09 5:27 PM Page 431 For example, in your application’s master page, add the following <script> tag at the top of the <head> section: <head runat="server"> <script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" t ype="text/javascript"></script> <! Leave rest as before > </head> jquery-1.3.2.min.js is the minified version, which means that comments, long variable names, and unnecessary whitespace have been stripped out to reduce download times. If you want to read and understand jQuery’s source code, read the nonminified version ( jquery-1.3.2.js) instead. If you like, you can get the latest version of jQuery from http://jquery.com/. Download the core jQuery library file, put it in your application’s /Scripts folder, and then reference it as just shown. At the time of writing, there is no newer version than 1.3.2. INTELLISENSE SUPPORT FOR JQUERY Would you like IntelliSense with that? Providing IntelliSense for a truly dynamic language such as JavaScript is fundamentally difficult, because functions can be added to and removed from individual object instances at runtime, and all functions can return anything or nothing. Visual Studio 2008 tries its best to figure out what’s going on, but it only really works well if you create a .vsdoc file containing hints about how your JavaScript code works. The Visual Studio team has collaborated with the jQuery team to produce a special .vsdoc file that greatly improves IntelliSense support for jQuery. This file, jquery-1.3.2-vsdoc.js, is already included in your application’s /Scripts folder by default (newer versions may become available at http://docs.jquery.com/Downloading_jQuery). To use it, just place a reference to it. For exam- ple, place the following line inside the <asp:PlaceHolder> in your master page’s <head> section: <% /* %><script src="~/Scripts/jquery-1.3.2-vsdoc.js"></script><% */ %> Note that this <script> tag is merely a hint for Visual Studio: it will never be rendered to the browser, because it’s commented out with a server-side comment. So, reference the file simply using its virtual path as shown, and don’t resolve its virtual path using Url.Content() as you do with other <script> tags. If you’re using partial views (ASCX files), then unfortunately you need to duplicate this line at the top of each one, because ASCX files aren’t associated with any master page. Hopefully this slightly awkward setup will be streamlined in a future version of Visual Studio. You can alread y do wnload a patch that tells Visual Studio to find *-vsdoc.js files automa tically, but it doesn’t help if you import the main jQuery file using Url.Content(), nor does it solve the problem with ASCX files. F or more details and to download the pa tch, see Scott Guthrie’ s blog post at http://tinyurl.com/ jQIntelliSense . CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING432 10078ch12.qxd 4/9/09 5:27 PM Page 432 Basic jQuery Theory At the heart of jQuery is a powerful JavaScript function called jQuery(). You can use it to query your HTML page’s DOM for all elements that match a CSS selector. For example, j Query("DIV.MyClass") f inds all the d iv s in your document that have the CSS class M yClass . jQuery() returns a jQuery-wrapped set: a JavaScript object that lists the results and has many extra methods you can call to operate on those results. Most of the jQuery API consists of such methods on wrapped sets. For example, jQuery("DIV.MyClass").hide() makes all the matching divs suddenly vanish. For brevity, jQuery provides a shorthand syntax, $(), which is exactly the same as calling jQuery(). 7 Table 12-3 gives some more examples of its use. Table 12-3. Simple jQuery Examples As you can see, this is extremely concise. Writing the same code without jQuery would take many lines of JavaScript. The last two examples demonstrate two of jQuery’s important features: CSS 3 support: When supplying selectors to jQ uer y , y ou can use the vast major ity of CSS 3–compliant syntax, regardless of whether the underlying browser itself supports it. This includes pseudoclasses such as :has(child selector), :first-child, :nth-child, and :not(selector), along with attribute selectors such as *[att='val'] (matches nodes Code Effect $("P SPAN").addClass("SuperBig") Adds a CSS class called SuperBig to all <span> nodes that are contained inside a <p> node. $(".SuperBig").removeClass("SuperBig") Removes the CSS class called SuperBig from all nodes that have it. $("#options").toggle() Toggles the visibility of the element with ID options. (If it’s visible, it will be hidden; if it’s alr eady hidden, it will be sho wn.) $("DIV:has(INPUT[type='checkbox']:disabled)"). prepend("<i>Hey!</i>") Inserts the HTML markup <i>Hey!</i> at the top of all divs that contain a disabled check box. $("#options A").css("color", "red").fadeOut() Finds any hyperlink tags (i.e., <a> tags) con- tained within the element with ID options, sets their text color to red, and fades them out of view by slowly adjusting their opacity to zero. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING 433 7. In JavaScript terms, that is to say $ == jQuery (functions are also objects). If you don’t like the $() syntax—per haps because it clashes with some other JavaScript library you’re using (e.g. Prototype, which also defines $)—you can disable it by calling jQuery.noConflict(). 10078ch12.qxd 4/9/09 5:27 PM Page 433 with attribute att="val"), sibling combinators such as table + p (matches paragraphs immediately following a table), and child combinators such as body > div (matches divs that are immediate children of the <body> node). Method chaining: Almost all methods that act on wrapped sets also return wrapped sets, s o you can chain together a series of method calls (e.g., $ (selector). a bc ( ). d ef ( ). g hi ( ) — permitting very succinct code). Over the next few pages, you’ll learn about jQuery as a stand-alone library. After that, I’ll demonstrate how you can use many of its features in an ASP.NET MVC application. ■Note This isn’t intended to be a complete reference to jQuery, because it’s separate from ASP.NET MVC. I will simply demonstrate jQuery working with ASP.NET MVC without documenting all the jQuery method calls and their many options—you can easily look them up online (see http://docs.jquery.com/ or http://visualjquery.com/). For a full guide to jQuery, I recommend jQuery in Action , by Bear Bibeault and Yehuda Katz (Manning, 2008). A QUICK NOTE ABOUT ELEMENT IDS If you’re using jQuery or in fact writing any JavaScript code to work with your ASP.NET MVC application, you ought to be aware of how the built-in input control helpers render their ID attributes. If you call the text box helper as follows: <%= Html.TextBox("pledge.Amount") %> This will render <input id="pledge_Amount" name="pledge.Amount" type="text" value="" /> Notice that the element name is pledge.Amount (with a dot), but its ID is pledge_Amount (with an underscore). When rendering element IDs, all the built-in helpers automatically replace dot characters with underscores. This is to make it possible to reference the resulting elements using a jQuery selector such as $("#pledge_Amount"). Note that it wouldn’t be valid to write $("#pledge.Amount"), because in jQuery (and in CSS) that would mean an element with ID pledge and CSS class Amount. If you don’t like underscores and want the helpers to replace dots with some other character , such as a dollar symbol, you can configure an alternative replacement as follows: HtmlHelper.IdAttributeDotReplacement = "$"; You should do this once, during application initialization. For example, add the line to Application_Start() in your Global.asax.cs file. However, underscores work fine, so you probably won’t need to change this setting. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING434 10078ch12.qxd 4/9/09 5:27 PM Page 434 Waiting for the DOM Most browsers will run JavaScript code as soon as the page parser hits it, before the browser h as even finished loading the page. This presents a difficulty, because if you place some JavaScript code at the top of your HTML page, inside its <head> section, then the code won’t immediately be able to operate on the rest of the HTML document—the rest of the document hasn’t even loaded yet. Traditionally, web developers have solved this problem by invoking their initialization code from an onload handler attached to the <body> element. This ensures the code runs only after the full document has loaded. There are two drawbacks to this approach: • The <body> tag can have only one onload attribute, so it’s awkward if you’re trying to combine multiple independent pieces of code. • The onload handler waits not just for the DOM to be loaded, but also for all external media (such as images) to finish downloading. Your rich user experience doesn’t get started as quickly as you might expect, especially on slow connections. The perfect solution is to tell the browser to run your startup code as soon as the DOM is ready, but without waiting for external media. The API varies from one browser to the next, but jQuery offers a simple abstraction that works on them all. Here’s how it looks: <script> $(function() { // Insert your initialization code here }); </script> By passing a JavaScript function to $(), such as the anonymous function in the preceding code, you register it for execution as soon as the DOM is ready. You can register as many such functions as you like; however, I normally have a single $(function() { }); block near the top of my view or control template, and I put all my jQuery behaviors into it. You’ll see that technique throughout this chapter. Event Handling Ever since Netscape Navigator 2 (1996), it’s been possible to hook up JavaScript code to handle client-side UI events (such as click, keydown, and focus). For the first few years, the events API was totally inconsistent from one browser to another—not only the syntax to register an event, but also the event-bubbling mechanisms and the names for commonly used event properties (do y ou want pageX, screenX, or clientX?). I nter net Explorer was famous for its pathological determination to be the odd one out every time. Since those dark early days, modern browsers have become . . . no better at all! We’re still in this mess more than a decade later, and even though the W3C has ratified a standard events API (see www.w3.org/TR/DOM-Level-2-Events/events.html), few browsers support much of it. And in today’s world, where Firefox, iPhones, Nintendo Wiis, and small cheap laptops running Linux are all commonplace, your application needs to support an unprecedented diversity of browsers and platforms. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING 435 10078ch12.qxd 4/9/09 5:27 PM Page 435 jQuery makes a serious effort to attack this problem. It provides an abstraction layer a bove the browser’s native JavaScript API, so your code will work just the same on any jQuery-supported browser. Its syntax for handling events is pretty slick. For example, $("img").click(function() { $(this).fadeOut() }) causes each image to fade out when you click it. (Obviously, you have to put this inside <script></script> tags to make it work.) ■Note Wondering what $(this) means? In the event handler, JavaScript’s this variable references the DOM element receiving the event. However, that’s just a plain old DOM element, so it doesn’t have a fadeOut() method. The solution is to write $(this), which creates a wrapped set (containing just one element, this) endowed with all the capabilities of a jQuery wrapped set (including the jQuery method fadeOut()). Notice that it’s no longer necessary to worry about the difference between addEventListener() for standards-compliant browsers and attachEvent() for Internet Explorer 6, and we’re way beyond the nastiness of putting event handler code right into the element definition (e.g., <img src=" " onclick="some JavaScript code"/>), which doesn’t support multiple event handlers. You’ll see more jQuery event handling in the upcoming examples. Global Helpers Besides methods that operate on jQuery-wrapped sets, jQuery offers a number of global prop- erties and functions designed to simplify Ajax and work around cross-browser scripting and box model differences. You’ll learn about jQuery Ajax later. Table 12-4 gives some examples of jQuery’s other helpers. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING436 Table 12-4. A Few Global Helper Functions Provided by jQuery Method Description $.browser Tells you which browser is running, according to the user-agent string. You’ll find that one of the following is set to true: $.browser.msie, $.browser.mozilla, $.browser.safari, or $.browser.opera. $.browser.version Tells you which version of that browser is running. $.support Detects whether the browser supports various facilities. For example, $.support. boxModel deter mines whether the curr ent fr ame is being r endered according to the W3C standar d bo x model. * Check the jQ uer y documentation for a full list of what capabilities $.support can detect. $.trim(str) Returns the string str with leading and trailing whitespace removed. jQuery provides this useful function because, strangely, there’s no such function in regular JavaScript. $.inArray(val, arr) R etur ns the first index of val in the arr ay arr. jQ uer y pr o vides this useful function because Internet Explorer, at least as of version 7, doesn’t otherwise have an array.indexOf() function. * The box model specifies how the browser lays out elements and computes their dimensions, and how padding and border styles ar e factored into the decision. This can vary according to browser version and which DOCTYPE your HTML page declares. Sometimes you can use this information to fix layout differences between browsers by making slight tweaks to padding and other CSS styles. 10078ch12.qxd 4/9/09 5:27 PM Page 436 This isn’t the full set of helper functions and properties in jQuery, but the full set is actu- a lly quite small. jQuery’s core is designed to be extremely tight for a speedy download, while also being easily extensible so you can write a plug-in to add your own helpers or functions that operate on wrapped sets. Unobtrusive JavaScript You’re almost ready to start using jQuery with ASP.NET MVC, but there’s just one more bit of theory you need to get used to: unobtrusive JavaScript. What’s that then? It’s the principle of keeping your JavaScript code clearly and physically separate from the HTML markup on which it operates, aiming to keep the HTML portion still functional in its own right. For example, don’t write this: <div id="mylinks"> <a href="#" onclick="if(confirm('Follow the link?')) location.href = '/someUrl1';">Link 1</a> <a href="#" onclick="if(confirm('Follow the link?')) location.href = '/someUrl2';">Link 2</a> </div> Instead, write this: <div id="mylinks"> <a href="/someUrl1">Link 1</a> <a href="/someUrl2">Link 2</a> </div> <script type="text/javascript"> $("#mylinks a").click(function() { return confirm("Follow the link?"); }); </script> This latter code is better not just because it’s easier to read, and not just because it doesn’t inv olve repeating code fragments. The key benefit is that it’s still functional even for browsers that don’t support JavaScript. The links can still behave as ordinary links. There’s a design process you can adopt to make sure your JavaScript stays unobtrusive: • First, build the application or feature without using any JavaScript at all, accepting the limitations of plain old HTML/CSS, and getting viable (though basic) functionality. • After that, you’re free to layer on as much rich cross-browser JavaScript as you like— Ajax, animations . . . go wild!—just don ’ t touch the original markup. Preferably, keep y our scr ipt in a separ ate file , so as to r emind yourself that it’s distinct. You can radically enhance the application’s functionality without affecting its behavior when JavaScript is disabled. Because unobtrusive JavaScript doesn’t need to be injected at lots of different places in the HTML document, y our MV C view templates can be simpler , too. You certainly won’t find CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING 437 10078ch12.qxd 4/9/09 5:27 PM Page 437 yourself constructing JavaScript code using server-side string manipulation in a <% foreach( ) %> loop! jQuery makes it relatively easy to add an unobtrusive layer of JavaScript, because after you’ve built clean, scriptless markup, it’s usually just a matter of a few jQuery calls to attach sophisticated behaviors or eye candy to a whole set of elements. Let’s see some real-world examples. Adding Client-Side Interactivity to an MVC View Everyone loves a grid. Imagine you have a model class called MountainInfo, defined as follows: public class MountainInfo { public string Name { get; set; } public int HeightInMeters { get; set; } } You could render a collection of MountainInfo objects as a grid, using a strongly typed view template whose model type is IEnumerable<MountainInfo>, containing the following markup: <h2>The Seven Summits</h2> <div id="summits"> <table> <thead><tr> <td>Item</td> <td>Height (m)</td> <td>Actions</td> </tr></thead> <% foreach(var mountain in Model) { %> <tr> <td><%= mountain.Name %></td> <td><%= mountain.HeightInMeters %></td> <td> <% using(Html.BeginForm("DeleteItem", "Home")) { %> <%= Html.Hidden("item", mountain.Name) %> <input type="submit" value="Delete" /> <% } %> </td> </tr> <% } %> </table> </div> It’s not very exciting, but it works, and there’s no JavaScript involved. With some appropri- ate CSS and a suitable DeleteItem() action method , this will display and behave as shown in F igur e 12-6. CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING438 10078ch12.qxd 4/9/09 5:27 PM Page 438 Figure 12-6. A basic grid that uses no JavaScript To implement the Delete buttons, it’s the usual “multiple forms” trick: each Delete button is contained in its own separate <form>, so it can invoke an HTTP POST—without JavaScript— to a different URL according to which item is being deleted. (We’ll ignore the more difficult question of what it means to “delete” a mountain.) Now let’s improve the user experience in three ways using jQuery. None of the following changes will affect the application’s behavior if JavaScript isn’t enabled. Impro vement 1: Zebra-Striping This is a common web design convention: you style alternating rows of a table differently, cre- ating horizontal bands that help the visitor to parse your grid visually. ASP.NET’s DataGrid and GridView controls have built-in means to achieve it. In ASP.NET MVC, you could achieve it by rendering a special CSS class name on to every second <TR> tag, as follows: <% int i = 0; %> <% foreach(var mountain in Model) { %> <tr <%= i++ % 2 == 1 ? "class='alternate'" : "" %>> but I think you’ll agree that code is pretty unpleasant. You could use a CSS 3 pseudoclass: tr:nth-child(even) { background: silver; } but you’ll find that very few browsers support it natively (only Safari at the time of writing). So, bring in one line of jQuery. You can add the following anywhere in a view template, such as in the <head> section of a master page, or into a view template near to the markup upon which it acts: <script type="text/javascript"> $(function() { $("#summits tr:nth-child(even)").css("background-color", "silver"); }); </script> CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING 439 10078ch12.qxd 4/9/09 5:27 PM Page 439 That works on any mainstream browser, and produces the display shown in Figure 12-7. N otice how we use $ (function() { }); t o register the initialization code to run as soon as the DOM is ready. ■Note Throughout the rest of this chapter, I won’t keep reminding you to register your initialization code using $(function() { });. You should take it for granted that whenever you see jQuery code that needs to run on DOM initialization, you should put it inside your page’s $(function() { }); block. Figure 12-7. The zebra-striped grid To make this code tidier, you could use jQuery’s shorthand pseudoclass :even, and apply a CSS class: $("#summits tr:even").addClass("alternate"); Improvement 2: Confirm Before Deletion It’s generally expected that you’ll give people a warning before you perform a significant, irrevocable action, such as deleting an item. 8 Don’t render fragments of JavaScript code into onclick=" " or onsubmit=" " attributes—assign all the event handlers at once using jQuery. Add the following to your initialization block: $("#summits form[action$='/DeleteItem']").submit(function() { var itemText = $("input[name='item']", this).val(); return confirm("Are you sure you want to delete '" + itemText + "'?"); }); CHAPTER 12 ■ AJAX AND CLIENT SCRIPTING440 8. Better still, give them a way of undoing the action even after it has been confirmed. But that’s another topic . 10078ch12.qxd 4/9/09 5:27 PM Page 440 [...]... in an ASP.NET MVC application First, you saw ASP.NET MVC s built-in Ajax.* helpers, which are very easy to use but have limited capabilities Then, you got an overview of jQuery, which is enormously powerful but requires a fair knowledge of JavaScript By reading so far into this book, you’ve now learned about almost all of the MVC Framework s features What’s left is to understand how ASP.NET MVC fits... with ASP NET MVC, because the MVC Framework doesn’t interfere with your HTML structure or element IDs, and there are no automatic postbacks to wreck a dynamically created UI This is where MVC s “back to basics” approach really pays off jQuery isn’t the only popular open source JavaScript framework (though it seems to get most of the limelight at present) You might also like to check out Prototype, MooTools,... picture, such as how to deploy your application to a real web server, and how to integrate with core ASP.NET platform features This begins in the next chapter, where you’ll consider some key security topics that every ASP.NET MVC programmer needs to know about 10078ch13.qxd 3/26/ 09 12: 49 PM CHAPTER Page 4 59 13 Security and Vulnerability Y ou can’t go far as a web developer without a solid awareness of... 556 .94 M, ClosingPrice = 558.20M, Rating = "A+" }}; else return null; } } 4 49 10078ch12.qxd 450 4 /9/ 09 5:27 PM Page 450 CHAPTER 12 I AJAX AND CLIENT SCRIPTING In case you haven’t seen JSON data before, this action method sends the following string: {"OpeningPrice":556 .94 ,"ClosingPrice":558.2,"Rating":"A+"} This is JavaScript’s native “object notation” format—it actually is JavaScript source NET MVC. .. on the Web today, and see how your MVC application can defend against them 2 One such tool is Internet Explorer Developer Toolbar, available at http://tinyurl.com/2vaa52 or http://www.microsoft.com/downloads/details.aspx?familyid=e59c 396 4-672d-4511-bb3e2d5e1db91038&displaylang=en (Clean URLs? Overrated! What you want is a nice GUID, mate.) 10078ch13.qxd 3/26/ 09 12: 49 PM Page 463 CHAPTER 13 I SECURITY... JS—they’ll all play nicely with ASP NET MVC, and you can even use more than one of them at the same time Each has different strengths: Prototype, for instance, enhances JavaScript’s object-oriented programming features, while Ext JS provides spectacularly rich and beautiful UI widgets Dojo has a neat API for offline client-side data storage Reassuringly, all of those projects have attractive Web 2.0–styled... around ,9 depending on how you’ve structured your HTML document Figure 12-11 shows the result 9 The element you parse out of response by calling $("#summits", response) must not be a direct child of the element, or it won’t be found That’s rarely a problem, but if you do want to find a toplevel element, you should replace this with $(response).filter("div#summits") 10078ch12.qxd 4 /9/ 09 5:27... to consume your service using Ajax requests 451 10078ch12.qxd 452 4 /9/ 09 5:27 PM Page 452 CHAPTER 12 I AJAX AND CLIENT SCRIPTING entire UI That’s a valid architecture for a very modern web application (assuming you don’t also need to support non-JavaScript clients) You’d benefit from all the power and directness of the ASP.NET MVC Framework but would skip over the view engine entirely Fetching XML... returns false so that the doesn’t get submitted in the traditional way Altogether, it produces the behavior shown in Figure 12-13 10078ch12.qxd 4 /9/ 09 5:27 PM Page 4 49 CHAPTER 12 I AJAX AND CLIENT SCRIPTING Figure 12-13 A trivial hijaxed form inserting its result into the DOM I Note This example doesn’t provide any sensible behavior for non-JavaScript-supporting clients For those, the whole page... OpeningPrice = 556 .94 M, ClosingPrice = 558.20M, Rating = "A+" } is C# source code 11 You need to surround the JSON string with parentheses, as in eval("(" + str + ")") If you don’t, you’ll get an Invalid Label error and eventually start to lose your mind 10078ch12.qxd 4 /9/ 09 5:27 PM Page 451 CHAPTER 12 I AJAX AND CLIENT SCRIPTING Then change the hijaxing code to display each StockData property in the . in an ASP. NET MVC application. ■Note This isn’t intended to be a complete reference to jQuery, because it’s separate from ASP. NET MVC. I will simply demonstrate jQuery working with ASP. NET MVC. jQuery with ASP. NET MVC and has said they will include it in Visual Studio 2010, even though it isn’t a Microsoft product. So, what’s all the fuss about? Using jQuery with ASP. NET MVC Write less,. very interesting to ASP. NET MVC developers. With ASP. NET MVC, you’re free to use any Ajax or JavaScript library. The most popular option, judging by the overwhelming roar of approval coming from

Ngày đăng: 06/08/2014, 08:22

TỪ KHÓA LIÊN QUAN