I have a partial that I am reloading after an Ajax call. The following code works:
public PartialViewResult AddPreferredPosition(string playerID, int positionID)
{
playerService.AddPlayerPreferredPosition(Guid.Parse(playerID), positionID);
PreferredPositionsModel model = new PreferredPositionsModel();
model.PreferredPositions = playerService.GetPlayerPreferredPositions(Guid.Parse(playerID));
model.Positions = playerService.GetPositions();
return PartialView("~/Areas/Admin/Views/Player/Partials/PreferredPositions.cshtml", model);
}
along with:
var params = {};
params.playerID = $('#PlayerID').val();
params.positionID = $("select[name='ddlPositionID'] option:selected").val();
$.ajax({
type: 'POST',
url: '/Admin/Player/AddPreferredPosition',
data: params,
success: function (data) {
$('#PreferredPositions').html(data);
}
});
I want to alter it slightly to manage my error handling further to something like this:
public ActionResult AddPreferredPosition(string playerID, int positionID)
{
try
{
playerService.AddPlayerPreferredPosition(Guid.Parse(playerID), positionID);
PreferredPositionsModel model = new PreferredPositionsModel();
model.PreferredPositions = playerService.GetPlayerPreferredPositions(Guid.Parse(playerID));
model.Positions = playerService.GetPositions();
return Json(new
{
Success = true,
HTML = PartialView("~/Areas/Admin/Views/Player/Partials/PreferredPositions.cshtml", model)
}, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new
{
Success = false,
ErrorMessage = ex.Message,
InnerException = (ex.InnerException != null ? ex.InnerException.Message : ""),
ex.StackTrace
});
}
}
with:
$.ajax({
type: 'POST',
url: '/Admin/Player/AddPreferredPosition',
data: params,
success: function (data) {
if (data.Success) {
$('#PreferredPositions').html(data.HTML);
} else {
alert(data.ErrorMessage);
console.log(data.StackTrace);
}
}
});
However, when I do this then data.HTML
is not the compiled partial view's HTML, as it is in the working example.
What am I doing wrong?