Reputation: 1318
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>
}
Upvotes: 0
Views: 1727
Reputation: 39
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:
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>
}
Upvotes: 0
Reputation: 3182
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 })
Upvotes: 1
Reputation: 66
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
Upvotes: 1