views:

8

answers:

1

This is my first MVC app and I'm not sure how to use a parameter to filter the returned data. I'm using MVC2 and Visual Studio 2008.

How do I filter view results based on user input? I want the user to be able to enter an ID number in a textbox and then click a button to get results filtered by the ID they entered.

here is my Controller

    public class HelloWorldController : Controller
    {
        UAStagingEntities db = new UAStagingEntities();

        public ActionResult Index()
        {
            var depot = from m in db.CSLA_DEPOT
                        where m.DEPOT_ID==10057
                        select m;

            return View(depot.ToList());

        }
    }

how do I change this to accept a paramter instead of a hard coded ID?

A: 

Initially try getting it working from the address bar in your browser.

Change the code to receive an Id parameter:

public ActionResult Index(int Id)
{
    var depot = from m in db.CSLA_DEPOT
                where m.DEPOT_ID==id
                select m;

    return View(depot.ToList());

}

Then you should be able to call .../controller/action/id

Next add an actionLink to your webpage to call this action

Clicktricity
thanks I got the parameter working in the address bar. How do I set up the actionLink?
Joshua Slocum
in your view page, something like: <%= Html.ActionLink("click me", "Index", new { id = <SelectedId> } ) %>
Clicktricity