0

I am trying to rewrite the default MVC Index controller that displays the list of records from a database. I am trying to have an order by function on 1 of the headings AND the ability to search on the page:

Controller code:

public ActionResult Index(String sortOrder, String searchString) {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
        var profiles = from s in db.Profiles
                       select s;

        if (!String.IsNullOrEmpty(searchString))
        {
            profiles = profiles.Where(d => d.lastName.ToUpper().Contains(searchString.ToUpper()));
        }
        switch (sortOrder)
        {
            case "Name_desc":
                profiles = profiles.OrderByDescending(s => s.lastName);
                break;
            default:
                profiles = profiles.OrderBy(s => s.lastName);
                break;
        }
        return View(profiles.ToList());
    }

View Code:

<th>
    @Html.ActionLink("Last Name", "Index", new { sortOrder = "Name_desc" })
</th>

I have been trying various ways, but it seems as if I am not succeeding with the @html.actionLink calling the controller method

Followed this tutorial exact but not working: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

Gman16
  • 183
  • 2
  • 6
  • 15
  • view source in the browser will show what is actually being invoked. Set a break point in the controller method at viewbag and see if breakpoint hit. – Mike Beeler Dec 03 '13 at 22:00
  • 1
    Is the view with the action link returned from an action in the same controller? – Ant P Dec 03 '13 at 22:02
  • @AntP Yes it is the same controller. The Index method previously worked when I just had the searching functionality. When I tried to include the order by/sorting functionality, it is not working – Gman16 Dec 04 '13 at 18:14
  • @MikeBeeler The breakpoint does not hit when they click the actionlink BUT it does it when they click the button to search – Gman16 Dec 04 '13 at 18:14

1 Answers1

0

So to explain how I solved the answer...

The actionResult was a HttpPost back. So it would only accept input when a button is clicked. It seems as if @HTML.actionlinks only call the view methods.

I simply created a new Index(String sortOrder, String searchString) method and the view code was @Html.ActionLink("Last Name", "/Index", new { sortOrder = ViewBag.NameSortParm})

Its messy, but solved my issue nonetheless

Gman16
  • 183
  • 2
  • 6
  • 15