1

I have a Device Management Application implemented in C# ASP.NET, and I have implemented the Searching and the Sorting functionality too in my application.

My problem is that I cannot sort my searching results, instead of after clicking on Name (to sort the results by name), it will sort all of the devices and not only the searching results.

In this controller, I show the devices without an owner (with UserID == null)

Here is my Controller:

public ActionResult deviceList(String sortBy, String search)
        {
            if (Session["userID"] != null)
            {
                ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
                ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
                ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";
                var devices = db.devices.AsQueryable();

                if (devices.Any(x => x.UserID == null))
                {
                    devices = db.devices.Where(x => x.UserID == null);

                    switch (sortBy)
                    {
                        case "Name desc":
                            devices = devices.Where(x => x.DName.Contains(search)).OrderByDescending(x => x.DName);
                            break;
                        ...
                        default:
                            devices = devices.Where(x => x.DName.Contains(search)).OrderBy(x => x.DName);
                            break;
                    }
                    return View(devices.ToList());
                }

The missing parts are the same sorting by type, manufacturer, etc.

Here is my View:

@using (Html.BeginForm("deviceList", "devices", FormMethod.Get))
                {
                    <label>
                        <b>Search:</b>
                        @Html.TextBox("Search")
                        <input type="submit" value="Search" />
                    </label>
                }
            </div>

            <table class="table table-striped custab">
                <thead>
                    <tr>
                        <th align="center">
                            @Html.ActionLink("Name", "deviceList", new { sortBy = ViewBag.SortNameParameter })
                        </th>
                        <th>
                            @Html.ActionLink("Manufacturer", "deviceList", new { sortBy = ViewBag.SortManufacturerParameter })
                        </th>
                        <th>
                            @Html.ActionLink("Type", "deviceList", new { sortBy = ViewBag.SortTypeParameter })
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DOS)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DOSVersion)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.DProcessor)
                        </th>
                        <th align="center">
                            @Html.DisplayNameFor(model => model.DRAM)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.user.userName)
                        </th>
                        <th>Action</th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.DName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DManufacturer)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DType)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DOS)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DOSVersion)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DProcessor)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DRAM)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.user.userName)
                            </td>

The search functionality works great, but after the search result if I click on the Name (to sort), the search parameters from URL disappear and it will sort my whole list instead of the search result.

Please help me find out my fault.

0

3 Answers 3

2

Your problem is obvious. when you click on sort links, you must send filter parameter as well.

So act as follow: Keep search parameter in ViewBag in deviceList action as well:

ViewBag.SearchParameter = search;
ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";

Then pass it same as sortBy in links:

@Html.ActionLink("Name", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortNameParameter  })
@Html.ActionLink("Manufacturer", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortManufacturerParameter })
@Html.ActionLink("Type", "deviceList", new { search = ViewBag.SearchParameter, sortBy = ViewBag.SortTypeParameter })

and also manage search parameter for textbox:

@Html.TextBox("search", ViewBag.SearchParameter)
Sign up to request clarification or add additional context in comments.

Comments

0

Just change search to Search on this Action

public ActionResult deviceList(String sortBy, String Search)
        {
            if (Session["userID"] != null)
            {
                ViewBag.SortNameParameter = String.IsNullOrEmpty(sortBy) ? "Name desc" : "";
                ViewBag.SortTypeParameter = sortBy == "Type" ? "Type desc" : "Type";
                ViewBag.SortManufacturerParameter = sortBy == "Manufacturer" ? "Manufacturer desc" : "Manufacturer";
                var devices = db.devices.AsQueryable();

                if (devices.Any(x => x.UserID == null))
                {
                    devices = db.devices.Where(x => x.UserID == null);

                    switch (sortBy)
                    {
                        case "Name desc":
                            devices = devices.Where(x => x.DName.Contains(Search)).OrderByDescending(x => x.DName);
                            break;
                        ...
                        default:
                            devices = devices.Where(x => x.DName.Contains(Search)).OrderBy(x => x.DName);
                            break;
                    }
                    return View(devices.ToList());
                }

Comments

0

It looks like when you click those links to sort by name/manufacturer/type it is not submitting the form so it is only redirecting you to that link. When you click the search button it is sending the form back to your controller with the search data, however the @Html.ActionLink(...) does not submit the form.

You will need to send the search text along with this link, maybe by binding the search text to your model and updating your links to include that. Try the following:

@Html.ActionLink("Name", "deviceList", new { sortBy = ViewBag.SortNameParameter, ViewBag.SearchString })

In your form you would need to update it to send the search string:

<label>
  <b>Search:</b>
  @Html.TextBox("Search", @ViewBag.SearchString)
  <input type="submit" value="Search" />
</label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.