0

I have made an ASP.NET MVC application which has three functionalities. From a list of data you can: - Search - Sort the results - And select from a dropdownlist

But it doesn't work properly yet. When I sort the search results after a search the the whole list is getting sorted, not only the search results. And I want to view the item that is selected out of the dropdownlist. But nothing happens when I select something in the dropdownlist.

The controller:

 public class AddressController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: Address
    public ActionResult Index(string address, string sortOrder, string searchString)
    {
        var AddressList = new List<string>();

        var AddressQry = from d in db.Adres
            orderby d.Address
            select d.Address;

        AddressList.AddRange(AddressQry.Distinct());
        ViewBag.address = new SelectList(AddressList);

        ViewBag.AddressSortParm = String.IsNullOrEmpty(sortOrder) ? "address_desc" : "";
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
        ViewBag.LongitudeSortParm = sortOrder == "Longitude" ? "longitude_desc" : "Longitude";
        ViewBag.LatitudeSortParm = sortOrder == "Latitude" ? "latitude_desc" : "Latitude";  

        var addresses = from s in db.Adres
                       select s;

        if (!String.IsNullOrEmpty(searchString))
        {
            addresses = addresses.Where(s => s.Address.Contains(searchString));

        }

        if (!String.IsNullOrEmpty(address))
        {
            addresses = addresses.Where(s => s.Address == address);

        }

        switch (sortOrder)
        {
            case "address_desc":
                addresses = addresses.OrderByDescending(s => s.Address);
                break;
            case "Date":
                addresses = addresses.OrderBy(s => s.Date);
                break;
            case "date_desc":
                addresses = addresses.OrderByDescending(s => s.Date);
                break;
            case "Longitude":
                addresses = addresses.OrderBy(s => s.Longitude);
                break;
            case "longitude_desc":
                addresses = addresses.OrderByDescending(s => s.Longitude);
                break;
            case "Latitude":
                addresses = addresses.OrderBy(s => s.Latitude);
                break;
            case "latitude_desc":
                addresses = addresses.OrderByDescending(s => s.Latitude);
                break;
            default:
                addresses = addresses.OrderBy(s => s.Address);
                break;
        }

        return View(addresses);
    }
}

The view

@model IEnumerable<Keuzevak.Models.Adres>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Addresses</h2>
<p>
 @Html.DropDownList("address", "All")
</p>


@using (Html.BeginForm())
{
<p>
    Search address: @Html.TextBox("SearchString")
    <input type="submit" value="Search" class="btn btn-primary" />
</p>
}

<table>
<tr>
    <th>
        @Html.ActionLink("Date", "Index", new {sortOrder =     ViewBag.DateSortParm})
    </th>
    <th>
        @Html.ActionLink("Longitude", "Index", new {sortOrder =    ViewBag.LongitudeSortParm})
    </th>
    <th>
        @Html.ActionLink("Latitude", "Index", new {sortOrder = ViewBag.LatitudeSortParm})
    </th>
    <th>
        @Html.ActionLink("Address", "Index", new {sortOrder = ViewBag.AddressSortParm})
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Longitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Latitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
    </tr>
}

2
  • does your action methord is hitting while giving request Commented Jun 7, 2016 at 16:02
  • You form only posts back the value of searchString (the values of address and sortOrder will be null) And you links only post back the value of sortOrder and the other values are null Commented Jun 7, 2016 at 23:42

3 Answers 3

1

You need to tell the view where it will be sending the data by specifying your controller action ..

You will need to wrap your fields within the form to send to your controller action method.

Something like ..

@using (Html.BeginForm("Index", "Address"))
{
    //Your dropdown here
    //Your search param
    //Your Sort

    //Your submit btn
}

I would use <input type "text" name="searchString" /> to send your text to the controller too.

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

1

You are not passing three parameter to action link but your action method is expecting three parameter

@Html.ActionLink("Last Name", "Index", new {address= ViewBag.address ,sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })

Comments

0

Consider putting you search results into a new list/collection. Looks reuse is happening with the

db.adres

for collection.

If you separate the search, sort, address into there own separate functions, you might even see this as cleaner code and be able to create a new list, based on the previous search criteria(controller action parameters).

To organize your criteria, added a new class with properties for SearchString, SortOrder, Address, like below:

public string SearchString { get; set; }
public string SortOrder {get; set; }
public string Address { get; set; }

Also, try using addresses.Find(searchString) instead for searching and sorting your list.

For the dropdown, make sure you are loading you dropdown list properly, with the right object, here's more information at this link:

http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

Typically for making something occur with dropdown list or something on the page you want to use a form, try using HTTP POST method or GET for retrieving information/data when submitting a form with the dropdown list.

    @using (Html.BeginForm("SearchItemChosen", "Index", FormMethod.Post)) {

    <fieldset>

            Addresses

            @Html.DropDownList("address")

        <p>

            <input type="submit" value="Submit" />

        </p>

    </fieldset>

} 

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.