Matt
Matt

Reputation: 1616

MVC5 Ajax returned Partial not being rendered

I have the following code. When I subit via Ajax, I can see the call go to the controller, and it returns the correct results. However, although it returns success, the partial is not being updated. Its almost certainly something stupid I am doing, but I have been starting at this so long now that Im going round in circles.

My suspicion is that I need to handle JSON data differently if I want to display it as a set of models in a partial. But as I am still finding my feet with MVC and ajax, its a lot of trial and error at present.

@{
System.Web.Mvc.Ajax.AjaxOptions ajaxopts = new AjaxOptions()
{
    HttpMethod = "Post",
    UpdateTargetId = "dlrsTable",
    OnBegin = "OnBegin",
    OnComplete = "OnComplete",
    OnSuccess = "OnSuccess",
    OnFailure = "OnFailure"
};
}
@using (Ajax.BeginForm("RefreshGrid", "Dealership", ajaxopts, new { id = "allDlrs" }))
{
@Html.Hidden("showArchived", false )
@Html.Hidden("ShowPage", 0)

@Html.AntiForgeryToken()
<div id="dlrTable">
    @{Html.RenderPartial("_allDealerships", Model);}
</div>
}

and my controller;

public async Task<JsonResult> RefreshGrid(bool? showArchived, int? ShowPage)
    {            
        try
        {
           //code elided, I can see it works and mappedDlrs contains the new data I want
            return new JsonResult(){ Data = mappedDlrs };
        }
        catch (Exception ex)
        {
            _log.Error(ex, string.Format("Error in DealershipController.AllDealerships - Session {0}", Session.SessionID));
return new JsonResult() { Data = "Error curred" };
        }            
    }

Upvotes: 0

Views: 103

Answers (1)

Tommy
Tommy

Reputation: 39807

Your update target and div IDs do not match.

UpdateTargetId = "dlrsTable"

<div id="dlrTable">

Note the missing s in the div id (or extra one in the UpdateTargetId)

Upvotes: 2

Related Questions