I am quite a newbie in MVC and I have got a question I just can't seem to work out.
I've got a page, let's call it index, that contains a partial page inside it:
// Some html + razor
<input type="button" value="Filter:"
onclick="FilterByUserRequestedValues()" />
<div id="filteredResultsDiv">
@Html.Partial("_filteredResultsPartial", Model)
</div>
The partial page is showing the data I want it to show filtered by certain parameters the user have selected at the index page.
This is the javascript function that executes once the user clicks on the button:
function FilterByUserRequestedValues() {
// Some code here
$.get("/PersonalArea/FilterPurchases", { 'dateFilter': dateFilter, 'selectedCategories': selectedCategories, 'selectedProducts': selectedProducts }, function (data) {
$('#filteredResultsDiv').html = data;
}, 'html');
My goal here is to call the FilterPurchases method that looks like this:
public ActionResult FilterPurchases(DateTime? dateFilter, string selectedCategories, string selectedProducts)
{
SetViewModel(dateFilter, selectedCategories, selectedProducts);
return PartialView("_filteredResultsPartial", viewModel);
}
When the jquery-get executes, the FilterPurchase is being called and as expected, I can see the call to the partial cshtml with the data filtered, however the index page view isn't being reloaded nor the filteredResultsDiv.
When I try debug it in Google Chrome, I think it does not get into the callback function at all. Nevertheless, even if it does, will it work?
filteredResultDivshould be replaced with theget()callback handler. Also, you are making a GET request so check that the browser is really making new requests to your server-side code and not reusing a cached result.