Why and when to use Ajax services and jQuery in ASP.NET MVC? -
may sounds stupid question i'm student came know mvc , find interesting. also, have little or no knowledge ajax , jquery. have been creating basic applications posting comment or blog post without using ajax services or jquery. i've come part see ajax services being called , custom jquery code being written.
let consider small example 1 can add comment on main page , after comment being written , submitted, appears on main page. imagine controller following functions
public class customajaxcontroller : controller { // // get: /customajax/ private static readonly list<string> comments = new list<string>(); public actionresult index() { return view(comments); } [httppost] public actionresult addcomment(string comment) { comments.add(comment); return redirecttoaction("index"); } } i know using list of string store comments not idea sake of simplicity , explain point i've used it. don't have model again same reason.
@model ienumerable<string> <h4>comments</h4> <ul id ="comments"> @foreach (var comment in model) { <li>@comment</li> } </ul> <form method="post" id="commentsform" action = "@url.action("addcomment")"> @html.textarea("comment", new{rows =5, cols =40}) <br/> <input type ="submit" value="add comment"/> </form> now, i've seen same thing being done using jquery , ajax requests. why should achieve same result, or how know right place use ajax requests.
but why should achieve same result, or how know right place use ajax requests.
ajax offers couple of benefits:
- it saves bandwidth because portion of page want updated sent on wire. in sample code reloading entire html if 1 comment added on single place
- it asynchronous meaning user can other things on page while waiting server side processing
you can use whenever want take advantage of 1 of things.
Comments
Post a Comment