Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a model "ppcc_matrix" which I've scaffolded (razor pages using EF). On my Index page, I display the entire table by default.
I have two filters created for the user to narrow down this table, including a MultiSelectList "DeptList" and a SelectList "EmployeeList".
When they make their selections and hit the "Submit button", it runs back through my OnGet to look at the selected values and if there are selections made, it retains the selectedValues to view. It then also uses LINQ to change my primary object to IQueryable and query the matching results to display the new table.

My SelectList "EmployeeList" is now filtering the table properly (even though it still isn't retaining my selection to view in the dropdown box). I have both my SelectList and MultiSelectList building my selections into the URL properly - But my MultiSelectList is doing nothing (not filtering the table to my selections).

This article has been helpful:
Select Lists in a Razor Pages Form[^]

What I have tried:

Index.cshtml (shortened):
HTML
<div class="table-responsive">
	<form asp-page="./Index" method="get">
		<div class="form-actions no-color form-inline">
			<p>
				<div>
					Dept Cd<br />
					<select multiple class="form-control" style="margin-right: 10px; margin-left: 5px;" asp-items="Model.DeptList" value="@Model.SelDepts"></select>
				</div>
				Name
				<select class="form-control col-md-2" style="margin-left: 5px;" asp-items="Model.EmployeeList" value="@Model.SelEmp">
					<option disabled selected style="display:none">--select--</option>
				</select>
				<input type="submit" value="Search" class="btn btn-primary btn-sm" style="margin-right: 5px; margin-left: 5px;" /> |
                <a asp-page="./Index" class="btn btn-dark btn-sm" style="margin-right: 5px; margin-left: 5px;">Back to full List</a>
			</p>
		</div>
	</form>
	<table class="table-condensed table-bordered table-striped nowrap" width="100%" cellspacing="0" style="margin-top: 10px">
		<thead>
			<tr>
				<th>
					<a asp-page="./Index"
					asp-route-selEmp="@Model.SelEmp"
					asp-route-selDepts="@Model.SelDepts"
					   >Department Cd</a>
				</th>
				<th>
					<a asp-page="./Index"
					asp-route-selEmp="@Model.SelEmp"
					asp-route-selDepts="@Model.SelDepts"
					   >Specialist</a>
				</th>
			</tr>
		</thead>
		<tbody>
			@foreach (var item in Model.ppcc_matrix)
			{
				<tr>
					<td>
						@Html.DisplayFor(modelItem => item.ppcc_deptCds.dept_cd)
					</td>
					<td>
						@Html.DisplayFor(modelItem => item.employees.employee_nm)
					</td>
				</tr>
			}
		</tbody>
	</table>
</div>


Index.cshtml.cs (shortened):
C#
[BindProperty]
public MultiSelectList DeptList { get; set; }
[BindProperty]
public SelectList EmployeeList { get; set; }
public int[] SelDepts { get; set; }
public int SelEmp { get; set; }

public async Task OnGetAsync(int selEmp, int[] selDepts, int curSelEmp, int[] curSelDepts)
    {

            DeptList = new MultiSelectList(_context.ppcc_deptCds, "Id", "dept_cd", SelDepts);
            EmployeeList = new SelectList(_context.employees, "Id", "employee_nm", SelEmp);

            if (curSelDepts.Any())
            {
                pageIndex = 1;
            }
            else
            {
                curSelDepts = selDepts;
            }

            if (curSelEmp != 0)
            {
                pageIndex = 1;
            }
            else
            {
                curSelEmp = selEmp;
            }

            SelEmp = curSelEmp;
            SelDepts = curSelDepts;

        IQueryable<ppcc_matrix> ppcc_matrixIQ = from s in _context.ppcc_matrix
                                                select s;

            if (curSelDepts.Any())
            {
                ppcc_matrixIQ = ppcc_matrixIQ.Where(s => s.ppcc_deptCdsID.Equals(curSelDepts));
            }

            if (curSelEmp != 0)
            {
                ppcc_matrixIQ = ppcc_matrixIQ.Where(s => s.employeesID.Equals(curSelEmp));
            }
    }
Posted
Updated 10-May-19 2:25am
v8
Comments
Richard Deeming 9-May-19 11:31am    
Try removing the @ from the start of your asp-for values.
Member 14164795 9-May-19 11:39am    
Progress.. Now when I make selection and Search, it uses the default value 0 instead of the id I've selected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900