I have the following code that populates two drop down lists, it all works great in the one view “BeachSearch” but I would like to re use it in a number of other views.
public ActionResult BeachSearch()
{
var db = new PeninsulaGuideEntities();
//Populate Coastline Dropdown
var coastlines = db.CoastLines.Select(c => new { c.CoastLineID, c.CoastLineName });
ViewBag.CoastLineId = new SelectList(coastlines.AsEnumerable(), "CoastLineID", "CoastLineName", 0);
//Populate Town Dropdown
var towns = (from bf in db.Towns
join f in db.Beaches on bf.TownID equals f.TownFK
select new
{
bf.TownID,
bf.TownName
}).Distinct();
ViewBag.TownId = new SelectList(towns.AsEnumerable(), "TownID", "TownName", 0);
return View();
}
How can I reuse this code, e.g.. a Partial View or an HTML Helper? If so, where should I put the file in my file structure remembering that all the views that will use it are derived from my HomeController.
In the view (assuming the BeachSearch method is in the HomeController) :
In the controller change this:
The sample uses asp.net MVC3 and a razor view