views:

43

answers:

1

Basically what I have is web services returning data in xml format. Once I do a call to a webservice to retrive data from web service function (GetUserList) i want to take that data and then dymaically display (no postback) the resulting information. I know several ways of doing this:

webservice sends data back to javascript, javascript then parses, replaces strings or text inside a div or child, or take the information retrieved and then put it into html table format through javascript.

Those are some of the idea's i've came up with, does anyone know the best practice to do this?

Using ASP.MVC (.Net)

+1  A: 

It depends entirely on your application. I've both had the action return pure data (usually JSON, not XML) and handled the display in the client and had the action return a partial view. For most complex display scenarios, I think the partial view route is easiest. Essentially, you are returning just the portion of the page (HTML) that is going to be updated. You use javascript in the AJAX callback handler to replace the appropriate element(s) on the page with the HTML you get back from the server. Note that you need to be careful with event handler binding when you do this -- it's almost always the right thing to do to use live handlers in jQuery 1.4+ and rebind all but click handlers in jQuery 1.3.

Example: Assumes that you are calling an MVC action method that returns a partial view. This will call the show action on the foo controller every 5 seconds and update the containerToUpdate (presumably a DIV), with the html returned.

  setInterval(
      function() {
          $.get( '<%= Url.Action( "show", "foo", new { id = Model.ID } ) %>',
                 function(html) {
                    $('#containerToUpdate').html( html );
                 });
      }, 5000
  );

Server side:

  [AcceptVerbs( HttpVerbs.Get )]
  public ActionResult Show( int id )
  {
      var model = ...

      if (this.Request.IsAjaxRequest())
      {
          return PartialView( model );
      }
      else
      {
          return View( model );
      }
  }

The full view (for non-AJAX) may not be necessary -- you may want to just display an error if the user shouldn't be accessing this except through AJAX. If you do support both, just render the partial inside the full view where it's needed so that you reuse the partial view code.

tvanfosson
can you give an example on what you mean by using the ajax call back handler?Also take into note that the Web service is being called on a javascript interval every X seconds.Appreciate the help
eqiz
I've added an example using an MVC, restful service on the back end.
tvanfosson
appreciate the assistance, greately appreciated
eqiz
Only change I'd make is Client Side I'd use $('#containerToUpdate').load() rather than $.get() as it's a bit more explicit.
Chao
@Chao - yes, that's probably better for a simple partial view. This pattern will work well if the method requires post processing on the client or it returns JSON, though.
tvanfosson