AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Adding additional search parameters

Open paul000 opened this issue 8 years ago • 3 comments

I'm developing a project based on the Contoso University tutorial and I'm trying to add an additional search parameter - it would be similar to searching from the Students index page and being able to search by Course Title to find all Students taking that Course. In the Student's index.cshtml.cs, "student IQ = studentIQ.Where(s => s.LastName.Contains(searchString)….. I cannot get (s.Course.Title.Contains(searchString) to work. What class do I add to so the path to Course.Title works?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

paul000 avatar Jun 24 '18 17:06 paul000

@paul000 you'll get a much quicker response by posting this on StackOverflow.

Rick-Anderson avatar Jun 24 '18 19:06 Rick-Anderson

@paul000 Hey Paul, I was curious about this too and was able to get working by doing this:

In Students/Index.cshtml.cs:

I added the property public string CurrentCourseFilter { get; set; } to the IndexModel class

In the OnGetAsync(...) method, I added the parameter string currentCourseFilter and after CurrentFilter = searchString; I added CurrentCourseFilter = currentCourseFilter;

Right after the part where the Docs filter the Students by their name I added another if statement:

// Filter by Course names
if (!String.IsNullOrEmpty(currentCourseFilter))
{
	studentIQueryable = studentIQueryable
		.Where(s=> s.Enrollments
			.Select(e => e.Course)
			.Any(c => c.Title.Contains(currentCourseFilter)));
}

Back in the Students/Index.cshtml I added the text box, the course names, and asp-route- values where need to keep the filter active when moving through pages or changing the sort order:

This is the search text box

<form asp-page="./Index" method="get">
    <div class="form-actions no-color">
        <p>
            Find by name:
            <input type="text" name="SearchString" value="@Model.CurrentFilter" />
            <input type="text" name="CurrentCourseFilter" value="@Model.CurrentCourseFilter" /> @* <-- Added this *@ 
            <input type="submit" value="Search" class="btn btn-default" /> |
            <a asp-page="./Index">Back to full List</a>
        </p>
    </div>
</form>

This is the table, I tried to cut it down a little

<table class="table">
    <thead>
    <tr>
        <th>
            <a asp-page="./Index" asp-route-sortOrder="@Model.NameSort"
               asp-route-currentFilter="@Model.CurrentFilter"
               asp-route-currentCourseFilter="@Model.CurrentCourseFilter"> @* <-- Added this *@
                @Html.DisplayNameFor(model => model.Student[0].LastName)
            </a>
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Student[0].FirstName)
        </th>
        <th>
            <a asp-page="./Index" asp-route-sortOrder="@Model.DateSort"
               asp-route-currentFilter="@Model.CurrentFilter"
               asp-route-currentCourseFilter="@Model.CurrentCourseFilter"> @* <-- Added this *@
                @Html.DisplayNameFor(model => model.Student[0].EnrollmentDate)
            </a>
        </th>
		@* V-- Added this *@
        <th>
            Courses
        </th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.Student)
    {
        <tr>
            <td>First name stuff</td>
            <td>Last name stuff</td>
            <td>Enrollment date stuff</td>
            @* V-- Added this *@
            <td>
                @{
                    string allCourseTitles = String.Empty;

                    foreach (var enrollment in item.Enrollments)
                    {
                        allCourseTitles += enrollment.Course.Title + ", ";
                    }

                    // Remove the last ", "
                    if (!String.IsNullOrEmpty(allCourseTitles))
                    {
                        allCourseTitles = allCourseTitles.Substring(0, (allCourseTitles.Length - 2));
                    }

                    @Html.DisplayName(allCourseTitles)
                }
            </td>
            <td>Edit/Details/Delete links</td>
        </tr>
    }
    </tbody>
</table>

And finally, at the bottom in the Paging controls area I added asp-route-currentCourseFilter="@Model.CurrentCourseFilter" to both the Previous and Next buttons

I'm not sure if writing the logic in the table row is frowned on or not, but I hope this helps! I also think some would prefer that I merged my new Where clause into the existing one, but I left it separate so it would easier to understand on it's own.

RyanTaite avatar Jul 19 '18 18:07 RyanTaite

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

Rick-Anderson avatar Nov 09 '18 03:11 Rick-Anderson