ddd
ddd

Reputation: 1399

Html.ActionLink problems

I am trying to provide a link to filter search results.

    <%= Html.ActionLink("Filter Results", "Index", new { page = Model.RestaurantList.PageIndex(), searchText = Model.SearchText, useFilter = true, filterOption = Model.FilterOption, filterText = Model.FilterText }, null)%>

The controller definition is as such

 public ActionResult Index(int? page, string searchText, bool useFilter, string filterText, string filterOption)

However when I debug this the values are not set properly, even the useFilter variable.

My link is rendered localhost/home/index/true?page=0

Any ideas how to fix this?

Upvotes: 0

Views: 1467

Answers (4)

Yoshi
Yoshi

Reputation: 91

I had a similar situation.

Something like this:

Html.ActionLink("Click me", "Index", "Student", 
        { ID = theclass.StudentID }, null) 

always renders as

localhost/myapp/Student/Index/1234

While I was experimenting, I noticed that Index part disappeared from the URL if I change the parameter name from ID to something like IDX

localhost/myapp/Student/?IDX=1234

It turns out that the parameter name ID is special because of the default route entry in Global.asax.cs (which sets up RESTful business object access pattern)

To work around this, I simply stopped using Index to take an ID and instead defined another method like this:

public ActionResult Detail(int ID)

After all, per RESTfull design principle, Index is meant for showing "listing of business objects" or some sort of collection of business object, not a single business object. So passing business object ID to Index is kind of violating the pattern.

Instead of twist the arm and force my awkward design, I changed to natural way.

I believe the best practice for designing Index entry point is to keep it parameter-less, or pass only filtration/sorting parameters.

Upvotes: 0

Mathias F
Mathias F

Reputation: 15891

However when I debug this the values are not set properly, even the useFilter variable. My link is rendered localhost/home/index/true?page=0

I think your useFilter parameter is actually rendered. Its mapped to the route I guess.

Try to set a hardcoded value for searchText

If it shows up, then you did not set your model in the controller.

Upvotes: 0

Buu
Buu

Reputation: 50335

The code segment looks fine. I think there are 2 possible errors:

  1. Wrong route information: check the routes.MapRoute(...) calls in global.ascx.cs/vb file to make sure a route for Home#Index action is properly configured
  2. Wrong controller: try use the overload of ActionLink which explicitly specifies a controller

If you still can't make it to work, you might want to post more info (like route mapping code, name of view/controller)

Upvotes: 2

Robert Harvey
Robert Harvey

Reputation: 180787

It looks like it should work.

Have you verified that the model fields you are passing to the ActionLink actually contain data?

Upvotes: 0

Related Questions