I started to created my project using MVC and it's really enjoying to create different projects using mvc.
So I created awhile ago the data table that filters data when you selected value in single dropdown
But I'm still curious about filtering data using multiple dropdown and two models in a single view
What I did... first, I created a class that I can be used to display my two data table using two models
This is my class in mvc
public class MyData
{
public IEnumerable<pmTA_ProjectCategory> ProjectCategory { get; set; }
public IEnumerable<pmTA_FundCategory> FundCategory { get; set; }
}
After creating my class, I created a code for two data table using one view for Index and view for partial view to call the two data table
this is the codes for view of Index and view of Partial View which the name is "MyPartialView"
For view of Index:
@using myProject.Models;
@model MyData
<div id="myPartialView">
@Html.Partial("MyPartialView",Model)
</div>
@if (Model.ProjectCategory != null) {
<table class="table table-bordered table-hover ">
<thead>
<tr>
<th>id</th>
<th>title </th>
<th>
description
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ProjectCategory)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
</tr>
}
</tbody>
</table>
}
For view of Partial View
<table id="myDataTable" class="table table-bordered table-hover ">
<thead>
<tr>
<th>id</th>
<th>code</th>
<th>
title
</th>
<th>
description
</th>
<th>--</th>
<th>--</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.FundCategory)
{
string selectedRow = "";
if (item.id == ViewBag.fund)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.code)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.ActionLink("Edit", "FundCategoryEdit", new { id =
item.id }, new { @class = "btn btn-primary" })
</td>
<td>
@Html.ActionLink("Select", "Index", new {
fund_category_id = item.id }, null)
</td>
</tr>
}
</tbody>
</table>
Inorder to Function my view I created codes in controller to display two data table, I declared the name of my class in the controller
this the codes in my controller, the first code is for the partial view datatable and the second code is for the View Index
for partial:
var viewModel = new MyData();
viewModel.FundCategory = (from p in db.pmTA_FundCategory
select new
{
id = p.id,
code = p.code,
title = p.title,
description = p.description,
status = p.status
}).ToList()
.Select(x => new pmTA_FundCategory()
{
id = x.id,
code = x.code,
title = x.title,
description = x.description,
status = x.status
});
For View of Index
if (fund_category_id != null)
{
ViewBag.fund = fund_category_id.Value;
viewModel.ProjectCategory = (from p in db.pmTA_ProjectCategory
join g in db.pmTA_FundCategory
on p.fund_category_id equals g.id
where p.fund_category_id == fund_category_id
select new
{
id = p.id,
title = p.title,
description = p.description,
title1 = g.title,
status = p.status
}).ToList()
.Select(x => new pmTA_ProjectCategory()
{
id = x.id,
title = x.title,
description = x.description,
title1 = x.title1,
status = x.status
});
}
return View(viewModel);
They are together in the Index controller but I seperated it in order you to understand my codes
The purpose of the if condition there is for the displaying of my View Index Data table when click the "Select" button of the partial data table based on there ID, you can use the codes above as your reference for showing another data table using other data table...
To display my multiple drop down I always used this codes
Codes for View of Index to display dropdowns
<div>
@Html.DropDownList("id", ViewBag.funds as SelectList, "Select...", new
{ @class = "form-control" })
</div>
<div>
@Html.DropDownList("projectcat", ViewBag.proj as SelectList,
"Select...", new
{ @class = "form-control" })
</div>
Codes for dropdown in controller to display the data inside of it based on the database data
for first and second dropdown
var data1 = from p in db.pmTA_FundCategory
select new
{
id = p.id,
code = p.code,
title = p.title,
description = p.description
};
SelectList list = new SelectList(data1, "id", "title");
ViewBag.funds = list;
var data2 = from p in db.pmTA_ProjectCategory
select new
{
id = p.id,
title = p.title,
description = p.description
};
SelectList list1 = new SelectList(data2, "id", "title");
ViewBag.proj = list1;
The problem is... How I gonna filter my Partial Data table using multiple drop down without using any plugins but with the help of javascript or any method to filter the data of data table?
Once I selected the value of my multiple dropdown my Partial Data table will display the data corresponds to the multiple dropdown selected...
ProjectCategoryin main view andFundCategoryin partial view right?ProjectCategorydata table with one drop down and otherFundCategorydata table with another drop down. means no any relation between both data table means when i changed option from first drop down then onlyProjectCategorydata table will be filter without any effect on otherFundCategorydata table and vice versa. let me know so I'll add an answer.