I have two partial views on my main index page with a filter dropdown. One partial view is for displaying a list of emps and the other is a roll-up for totaling emp.salary, etc. Currently, I can update my main emp grid via ajax when the filter dropdown is selected. The issue is that, my roll-up grid needs to be re-calculated each time a new filter is applied to the main emp grid and I’m not sure how to go about that.
Less details view models here is the code:
Controller: Person/Index()
// GET: /Person/
public ViewResult Index()
{
IList<Person> ppl = cs = db.Persons.ToList();
PersonRollup pr= new PersonRollup();
foreach (Person p in ppl)
{
pr.salary += ii.salary
}
PersonViewModel pvm = new PersonViewModel{Persons = ppl, PersonsRollup = pr}
return View(pvm);
}
View: Person/Index
@model Models.PersonVM>
@Html.ListBox("Filter")
<div id="PersonRollupGrid">@Html.Partial("_PersonsRollup", Model.PersonsRollup)</div>
<div id="PersonGrid">@Html.Partial("_Persons", Model.Persons)</div>
@* Filter selected from drop down *@
<script type="text/javascript">
$("select").multiselect({
click: function () {
$.get('@Url.Action("FilterPersons")', function (data) {
$('#PersonGrid').html(data);
});
// ???????????????????????????????????????????
// How can I update the rollup grid here....
//???????????????????????????????????????????
}
});
</script>
Shared View: _Persons
@model IEnumerable<Models.Person>
<table>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@item.name</td>
<td>@item.salary</td>
</tr>
</table>
Shared View: _PersonsRollup
@model Models.PersonRollup
<table>
<tr>
<th>TotalSalary</th>
</tr>
<tr>
<td>@model.salary</td>
</tr>
</table>
Controller: Filter
public ActionResult FilterPersons(string someFilteredData)
{
IList<Person> ppl = cs = db.Persons.Where(/*someFilteredData*/).ToList();
PersonRollup pr= new PersonRollup();
foreach (Person p in ppl)
{
pr.salary += ii.salary
}
PersonViewModel pvm = new PersonViewModel{Persons = ppl, PersonsRollup = pr}
return PartialView("_Persons", ppl.Persons);
}
1 Answer