Jana Weschenfelder
Jana Weschenfelder

Reputation: 860

WebGrid in Razor View not updated/refreshed after RedirectToAction after POST


I have a very similar problem to this post...
Webgrid not refreshing after delete MVC
Whatever I try, the WebGrid doesn't get updated/refreshed after inline editing/deletion although the viewModel contains the correct data before the View is shown...

Inside the Controller:

public ActionResult AddTermin(int? id, int? ser)
{
    ...
    viewModel = ...  //contains the correct (=updated) data
    return View(viewModel);  //contains the correct (=updated) data
}

Inside the View "AddTermin.cshtml":

@{
    var data = new List<Appointment>();
    if (Model != null && Model.RecurringAppointments != null)
    {
        data = Model.RecurringAppointments.Appointments;  //contains the correct (=updated) data
    }
    var grid = new WebGrid(source: data, rowsPerPage: 10);  //contains the correct (=updated) data
}

...

@grid.GetHtml(...)  //shows wrong (=old/previous/cached) data - ???

I had added the following entry in web.config already...

<system.web>
    <caching>
      <outputCache enableOutputCache="false" />
    </caching>
</system.web>

I also tried "[OutputCache(Duration = 0)]" already.

And I also tried "jQuery.ajaxSetup({ cache: false });".

And I also tried "ModelState.Clear();" already. But nothing works.

How can I get the WebGrid to show the updated data? Does someone have an idea?
Many thanks in advance!

Upvotes: 0

Views: 2262

Answers (1)

Jana Weschenfelder
Jana Weschenfelder

Reputation: 860

I could solve the problem now, I should mention that I'm using an AJAX POST. But since the action in the controller was called by the AJAX POST successfully and since there was a successfull Redirect to the AddTermin action via RedirectToAction then, I didn't want to believe that the AJAX POST might be the reason why the WebGrid isn't updated. But that was the reason. This solution helped me: View not refreshing after AJAX post

My solution looks like this now... Instead of "RedirectToAction", I use now:

[HttpPost]
public ActionResult UpdateAppointment(Appointment appointment)
{
    ...
    //old: 
    //return RedirectToAction("AddTermin");
    //new:
    var redirectUrl = new UrlHelper(Request.RequestContext).Action("AddTermin", "Veranstaltung");
    return Json(new { Url = redirectUrl });
}

And in the View "AddTermin.cshtml":

$(function () {
    $('.saveApp').on('click', function () {
        ...
        var Appointment = { ... };
        $.ajax({
            url: '/Veranstaltung/UpdateAppointment',
            data: JSON.stringify(Appointment),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                window.location.href = result.Url;
            },
            error: function (errorThrown) {
                ...
            }
        });
    });
})

It works fine for me.

Upvotes: 1

Related Questions