Reputation: 1305
I'm trying to learn ASP.NET MVC, so I did as test project this where I want to display in Index a list of cities and when you click on city to see the properties in those cities.
Then I did some changes in global.asax in order to come up with nice URLs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home-Browse",
"Accommodation-{city}",
new { controller = "Home", action = "Browse", city = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
So now after I see the list of cities and I click one I get a URL like: /Accommodation-Amsterdam
My problem is that I don't know how to do the inner join in order to tell the controller to show me the properties in the area selected. (I told you I'm just learning this)
My home controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using accomm2.Models;
namespace accomm2.Controllers
{
[HandleError]
public class HomeController : Controller
{
dataEntities _db;
public HomeController()
{
_db = new dataEntities();
}
public ActionResult Index()
{
ViewData.Model = _db.Cities.ToList();
return View();
}
public ActionResult Browse()
{
//ViewData.Model = _db.Properties.ToList();
var properties = from c in _db.Cities
from p in c.Properties
where c.CityId == CityId
select c;
return View(properties.ToList());
}
}
}
Could somebody help me with my Linq query or at least direct me to the right way?
Upvotes: 1
Views: 1049
Reputation: 4608
On your Browse controller, just add string city
as an argument, and it will magically be populated by MVC.
Upvotes: 1