Reputation: 1334
I am new to MVC. I want to filter my table using parameter from Dropdownlist:
My Model:
public class Document
{
[Key]
public int MasterAPID { get; set; }
public string CompanyID { get; set; }
public MasterCompany MasterCompany { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string DocumentNumber { get; set; }
}
public class MasterCompany
{
[Key]
public string CompanyID { get; set; }
public string CompanyName { get; set; }
}
My Controller:
private IRMAContext db = new IRMAContext();
// GET: MasterAPs
public ActionResult Index()
{
ViewBag.CompanyID = new SelectList(db.MasterCompanies, "CompanyID", "CompanyName");
var masterAPs = db.MasterAPs.Include(m => m.MasterCompany);
return View(masterAPs.ToList());
}
My View:
@Html.DropDownList("CompanyID", null, htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("oYear", Enumerable.Range(DateTime.Now.Year, 10).Select(x => new SelectListItem { Text = x.ToString() }), "Select Year", new { @class = "form-control" })
@Html.DropDownList("oMonth", Enumerable.Range(1, 12).Select(x => new SelectListItem { Text = x.ToString("00") }), "Select Month", new { @class = "form-control" })
<br>
<button type="submit">View</button>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.DocumentNumber)
</th>
</tr>
</table>
So On the table itself, I won't show the 3 parameters. Only the DocumentNumber
field. When user select company
, year
, month
then click the View button, automatically the table will only show the data that base on the selected parameter.
How can I achieve this thing?
really appreciated.
Upvotes: 0
Views: 55
Reputation: 91
You can add another action on your controller, and another view matching that action. For example:
// GET: Detials
public ActionResult Details(int companyId)
{
ViewBag.CompanyID = new SelectList(db.MasterCompanies, "CompanyID", "CompanyName");
var specificCompany = db.MasterAPs.Include(m => m.MasterCompany).Where(c => c.Id == companyId);
return View(specificCompany);
}
And edit the new Details.cshtml as needed with the data passed.
Upvotes: 1