Passing data from Ajax into partial view
I have a partial view that when the user clicks a button, some data is passed to the database and the results returned. The results must be displayed in another partial view. The results are obtained using the following Controller method and Ajax script:
public ActionResult GetResultData(Models.SelectedFilterValues selectedFilters)
{
resultData = resultRepository.GetResultData(
selectedFilters.Projects,
selectedFilters.ExperimentTypes,
selectedFilters.StudySet,
selectedFilters.Species,
selectedFilters.Strain,
selectedFilters.Department,
selectedFilters.Location);
return PartialView("Results", resultData);
}
function GetResultData(selectedProjects, selectedExperiments, selectedStudySets, selectedDepartments, selectedLocations, selectedSpecies, selectedStrain) {
$.ajax({
type: "GET",
url: "/Search/GetResultData",
data: { projects: selectedProjects, experimentTypes: selectedExperiments, studySet: selectedStudySets,
department: selectedDepartments, location: selectedLocations, species: sele开发者_StackOverflowctedSpecies, strain: selectedStrain
},
error: function (data) {
},
success: function (data) {
}
});
}
I keep getting the error when the data is returned into the Ajax method, is this because it is returning a partial view? What I want is for the Ajax method to accept the data, and then for me to pass that data into the new partial view. Is this possible?
Thanks.
I think you are mixing up client-side and server-side logic.
A partial view can contain logic that is executed on the server. Typically you would execute business logic in a Controller, and UI logic in the PartialView. For instance, any markup using Razor is actually executed on the server. The result then is HTML which is sent to the browser.
This HTML may contain client-side (JavaScript) code. So typically you would create a PartialView which contains JavaScript code that calls jQuery methods such as $.ajax(). When it does, it doesn't matter anymore how the JavaScript got to the browser -- as part of a PartialView or not, that doesn't matter. The JS code is executed on the client side, and it calls logic on the server side.
When the Ajax call returns data to the client side, the JS code there can then render the data into a grid, or apply a jQuery template, or do whatever with it that it wants. What it cannot do, is execute server-side PartialView code, because any PartialView has long since executed.
精彩评论