views:

756

answers:

2

Hi!!

I have a ActionResult named "MyPeriods(string dateSelected)" and in the end of it, I have a ViewData["periods"] = listOfPeriods (and after that, I have my return View(), fininshing up my method). When no "date" is passed, "date" is today, otherwise, "date" is the date passed as argument. And this "date" is important to select all the periods and events relationed to it.

So, my ActionResult is sending a list of periods to my View. In my View, I have:

<div id="divEventsPeriods">
   <% foreach(UserPeriod period in in (IEnumerable)ViewData["periods"])
       Response.Write("<div>PERIOD: " + period.hourBeg + " - " + period.hourEnd + "</div>");

      foreach(UserEvents event in period.UserPeriod) {
         Response.Write("<div>EVENT: " + event.date + "<br />");
         Response.Write("DESCRIPTION: " + event.description + "</div>");
      }
   %>
 </div>

So, when I select a date in jQuery DatePicker, this selected date is passed as an argument to my ActionResult and all the process occurs. And, in the end of all, my page refreshes rendering all the Periods and Events of the selected date. One Period may have many Events.

So, the question is: how can I pass this selected date to my ActionResult, making all process occurs and the page becomes updated without refreshing?

I've tried this on DatePicker onSelect option:

$.ajax({
     type: 'GET',
     url: '/Admin/Schedule/MyPeriods/?dateSelected=' + dateSelected,
     data: dateSelected
});

When I select a date, the $.ajax is called and debugging I could see that the selected date is passed correctly to my ActionResult and process occurs, but the page is not updated.

What am I doing wrong??

Thanks in advance!!

+3  A: 

You need to add a success callback to the ajax call that will accept the returned data and update your page via DOM manipulation, for example, by replacing the contents of a DIV with the HTML (presumably) returned by your action. I'm assuming here that your action returns a ContentResult. If you are returning a JsonResult instead, then you'll need use the result and do what is necessary to update the DOM.

Take a look at the jQuery AJAX options page for more information.

EDIT I decided to do this in one method, but using IsAjaxRequest(). Your client side code may not need to change at all with the exception of using the partial as noted below.

 public ActionResult ViewPage(DateTime dateSelected)
 {
      ....do some stuff...
      ViewData["dateSelected"] = GetPeriods( dateSelected );
      if (Request.IsAjaxRequest()) {
          return PartialView("PeriodDisplay", ViewData["dateSelected"]);
      }
      else {
          return View();
 }

 // common code to populate enumerable
 public IEnumerable<Period> GetPeriods( DateTime selected )
 {
      return ...data based on date...
 }

PeriodDisplay.ascx : this should be a strongly typed ViewUserControl using a model of IEnumerable.

 <%@ Page ... %> /// page definition...

   <% foreach(UserPeriod period in Model) { %>
      <div>PERIOD: <%= period.hourBeg + " - " + period.hourend %> </div>

   <% foreach(UserEvents event in period.UserPeriod) { %>
      <div>
           EVENT: <%= event.date %><br/>
           DESCRIPTION: <%= event.description %>
      </div>
   <% } %>

ViewPage.aspx

   ...
   <div id="divEventsPeriods">
      <% Html.RenderPartial( "PeriodDisplay", ViewData["periods"], null ); %>
   </div>
tvanfosson
I added a success callback... it worked partially.I added:sucess: function (html) { $("#divPeriodsEvents").append(html); }I also tried $("#divPeriodsEvents").text(html); but it was no good also.
AndreMiranda
Are you trying to replace the entire contents? If so you want to use $('#divPeriodsEvents").html(html). I'm assuming that the data you are getting back is correct and it's just the display of the data that's not working.
tvanfosson
Exactly! The data coming from the ActionResult is correct, but the display of this data is not correct. I also tried now $('#divPeriodsEvents").html(html), but this new content is inserted inside my page. I have my page and this new content appears inside my page, as a new page... page inside page
AndreMiranda
And I just want not to create a new page inside my page (very bizarre LOL!) and so, update this div content with the new one.
AndreMiranda
Use a partial view for the part of the page that you want to replace. Have two controller methods: one for your page and one for just the partial view. In your page view, use RenderAction to get the results of the method that returns the partial view. Call the partial view action from Ajax.
tvanfosson
tvanfosson, it didn't worked... the display was wrong because I am returning "return View()" in my ActionResult and of course that will render my whole page again!! I just want to get my ViewData and update my page content... I think I've got to work with getJSON but I don't know how!!
AndreMiranda
That's why I recommended moving the display of that particular data to a partial view and have an action that renders just that data via the partial. I'll try to come up with some sample code.
tvanfosson
A: 

Your action need to return either html or text or whatever you want to show, or JSON objects that you then process in your javascript. From the jQuery.ajax documentation:

 $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

The msg variable in this case is the html / text that you get back from your action. Your action can return a partial view or something as simple as <p>Date Selected feb 12th 2009</p>. You can also use ("#message").html(msg) to insert html into your webpage.

Morph