3

I have an MVC 3 application that is calling a Web Service it consumes to call Sharepoints API. The call takes roughly 6 seconds regardless of your results, so i have decided to add a async call using JQuery to provide the user with a waiting indicator. I have it almost competed, but i am unable to get the view to update with the returned data. I have narrowed it down to the view as when i get to the partial view that has the data, i have a break point in the ForEach loop and there is data in there and it is what i am expecting, but when i get to the View itself in the browser, the table is not populated. Here is my AJAX call that is started when a input button is clicked, which triggers great:

$(function () {
            $("#ajax-indicator").hide();
        });

        function getData() {
            $("#ajax-indicator").show();

            $.ajax({
                type: "POST",
                url: "/Home/MakeCall",
                // the URL of the controller action method 
                data: null,
                // optional data          
                success: function (result) {
                    // do something with result  
                    $("#ajax-indicator").toggle();
                },
                error: function (req, status, error) {
                    // do something with error     
                }
            });

        }

Here is within the same View, a the indicator div that gets hidden, the button and lastly the Render partial.

 <div id="ajax-indicator">
        <img alt="AJAX Indicator" src="<%= Url.Content("../../images/ajax-loader.gif") %>" />
    </div>
    <div id='button'>
        <% using (Html.BeginForm())
           { %>
               <input type="submit" name="submit" value="Get Data" onclick="getData();" />
           <% } 
        %>
    </div>


    <% Html.RenderPartial("MakeCall", ViewData["viewModel"]); %>

The partial page looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AsyncCallTest.Models.SharepointModel>" %>
<table border="1">
<tr>
<td>Folder Name</td>
<td>Link</td>
</tr>

<tr>

<% if (Model != null)
   {
       foreach (var item in Model.FolderList)
       {
       %>
    <td> FolderName: <%: item.FolderName%></td>
       <%
       }
   }

     %>
     </tr>
     </table>

When i look at the item.FolderName I have data there, but we are getting nada. i feel like i am missing something silly. Any ideas?

tereško
  • 58,060
  • 25
  • 98
  • 150
gcoleman0828
  • 1,541
  • 3
  • 30
  • 49

2 Answers2

1

You are not doing anything to show the data in the success except to toggle the wait indicator.

success: function (result) {
    // do something with result  
    $("#ajax-indicator").toggle();
},

You have to render the result that you got from the AJAX call. Add a div (a place holder) to show the results and then

success: function (result) {
    // do something with result  
    $("#placeHolderSelector").html(result); // Render the result
    $("#ajax-indicator").toggle();
},

Since this call is now async, you could remove

<% Html.RenderPartial("MakeCall", ViewData["viewModel"]); %>

from the view and instead have a placeholder instead

<div id="placeHolderSelector"></div>
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Ok i will give this a shot, so I am assuming then, that my partial View even though the model was being populated, it was after the view was rendered correct? – gcoleman0828 Nov 08 '11 at 03:00
  • @user479323 : If you want to show formatted data, like the way you were doing with Partial View, then you can use jQuery templates. jQuery templates, ease out the process of client side binding via async calls. – Pawan Mishra Nov 08 '11 at 03:06
  • @PawanMishra thanks I'm looking at the templates idea. I guess my biggest concern is loosing my strongly typed data and just shoving it into that .HTML property. Are there any better MVC solutions that would include strongly typed data and work better with MVC?? – gcoleman0828 Nov 08 '11 at 03:16
0

@PawanMishra thanks I'm looking at the templates idea. I guess my biggest concern is loosing my strongly typed data and just shoving it into that .HTML property. Are there any better MVC solutions that would include strongly typed data and work better with MVC??

For strongly typed implementation, you will have to go with pure "View Model" based approach. In one of my recent project, we decided not to use jQuery/JavaScript, rather use "ViewModel" objects for passing data between view and the controller. Yes, we got strongly typed behavior but we lost the charm of asynchronously updating the UI from jQuery. Since the application was meant for internal use, it was not much of a problem. Its a call for you to take as what you want i.e. a fluent UI using jQuery/JavaScript or strongly typed view-controller interaction.

Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • Thank you Pawan - Let me rephrase that question... is there a way to asynchronously update the view and still keep strongly typed data? And if so, what would be some examples of this? – gcoleman0828 Nov 08 '11 at 11:36
  • I don't think its possible. Asynchronously here means invoking action methods via jQuery and then using the response to update the UI or using async controllers. With the jQuery based approach, you will not get strong type checking. As I mentioned before, strong type checking is only possible, when you are using pure "ViewModel" object for communicating between your view and action. – Pawan Mishra Nov 08 '11 at 12:33
  • And there is no Asynchronous methods for passing between data and view within the actual MVC framework? There is an Ajax namespace? I had found Ajax.actionMethod, but it doesn't seem to be working. any thoughts on that or examples of using this method? – gcoleman0828 Nov 08 '11 at 12:53
  • I think "Ajax.actionMethod" is used for passing complex view model objects from jQuery to action methods. This(or something similar) method has been recently added in MVC 3. Before that people has difficulty in passing complex model objects, from script to action method. See this thread : http://stackoverflow.com/questions/4120212/mvc-ajax-json-post-to-controller-action-method – Pawan Mishra Nov 08 '11 at 12:58
  • Sorry to be a pest, but I just found something else that i thought i'd bounce off you. Asynchronous Controllers? See thing link; http://msdn.microsoft.com/en-us/library/ee728598%28VS.100%29.aspx It states that it is made for exactly what i am doing... long Web Service calls. But i'm not sure i see where i could give the user notification which is really what i'm trying to accomplish? Thanks for all the help – gcoleman0828 Nov 08 '11 at 13:11
  • What do you mean when you say user notification ? Is it some kind of constant progress update or one time message. If its continuous notification(also known as polling), then its a real challenging work.Basically your long running task executed via "Async Controller" will be executed on a separate thread and then you will have to constantly poll from UI i.e. jQuery/JavaScript to get the status. – Pawan Mishra Nov 08 '11 at 13:41
  • no not a constant poll. I will be calling a Web Service that takes 6 seconds to return. i am looking for a way to make the call and let the user know that something is happening without comprimising the Model. I think the Ajax.actionMethod with the LoadingElementId might work. http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=scott-allen&name=mvc3-building-ajax&mode=live&clip=0&course=aspdotnet-mvc3-intro – gcoleman0828 Nov 08 '11 at 14:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4802/discussion-between-pawan-mishra-and-user479323) – Pawan Mishra Nov 08 '11 at 15:22