Filter view data based on user input
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?
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
精彩评论