How to handle AJAX driven website in asp.net MVC (lots of views and partialviews)? -


i in process of putting new site make use of ajax pull through page content should user have javascript enabled.

so, in situation whereby every action method requires check see if request through ajax or not, straightforward. if request through ajax can return partialview, if not full view can returned.

with pattern though, i'll need create view , partialview every page on site. real difference between them going inclusion of masterpage.

am missing trick here is doubling of views way go?

thanks

edit - bit more info

lets had page accessed through /site/test. somewhere in js add hash url #/site/test. js watch hash changes , load partial views needed. if js not available though, entire view need returned.

so each page need view, include call renderpartial load partial view contain page content. so, every page there 2 files. seems there should cleaner way of doing this.

sergio, yes missing trick!!

you should organise page static content in - static. static page calls partial(s) give dynamic content. typically used in main page such (i'm using jquery per microsofts adopted stance on ajax now):

<asp:content id="content2" contentplaceholderid="maincontent" runat="server">     <h2>my header</h2>     <%--lots of stuff omitted--%>     <div id="dynamiclist"><%html.renderpartial("list"); %></div>     <%--also lots missed out here--%>     <input type="button" id="btnrefresh" value="refresh" /> </asp:content> 

this means partial rendered in initial request. subsequent refreshes call partial method in controller , repopulate 'dynmaiclist' div along lines of:

<script type="text/javascript">     // might have click or similar here invoke partial refresh     $(function() {          //click event (or other 'change' event)          $('#btnrefresh').click(function() {              dynamiclist();          });     });      function dynamiclist() {         // action/controller retruns partialview result         var url = '<%= url.action("list", "mycontroller") %>';         // merely wrapper method around jquery $ajax         sendajax(url, formparams(), beforedynamiclistquery, dynamiclistresponse);     }      function beforedynamiclistquery() {         $("#dynamiclist").fadeto('slow', 0.5);     }      function dynamiclistresponse(data) {         if (data.length != 0) {             if (data.indexof("error:") >= 0) {                 $("#dynamiclist_errmsg").html(data);             }             else {                 var selector = "#dynamiclist";                 $(selector).fadeto('slow', 1, function() {                     $(this).html(data);                 });             }         }     } </script> 

anyway, that's take on it!! ;)


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -