//here is Country controller which return country view and states view
public class CountryController : Controller
{
public ActionResult Index()
{
Data.TestEntities ent = new Data.TestEntities();
var country = ent.Countries.ToList();
return View("SearchView", country);
}
public ActionResult redirect(int? countryCode)
{
Data.TestEntities entity = new Data.TestEntities();
var states = entity.States.Where(s => s.CountryID == countryCode).ToList();
return View("ViewStates", states);
}
}
//here is country view
@model List<TestMVC4.Data.Country>
@{
ViewBag.Title = "SearchView";
}
<h2>SearchView</h2>
<table style="width:60%">
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>View States</th>
</tr>
@foreach (var country in Model)
{
<tr>
<td>@country.CountryID</td>
<td>@country.CountryName</td>
<td><big></big>@Html.ActionLink("ViewStates", "redirect", new {countryCode=country.CountryID })</td>
</tr>
}
</table>
// and after click on ViewStates StateView.chtml
@model List<TestMVC4.Data.State>
@{
ViewBag.Title = "ViewStates";
}
<h2>ViewStates</h2>
<table style="width:60%">
<tr>
<th>
State ID
</th>
<th>
State Name
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StateID)
</td>
<td>
@Html.DisplayFor(modelItem => item.StateName)
</td>
@*<td>
</td>*@
</tr>
}
<tr>
<td>
<p> @Html.ActionLink("back to search", "Index","Country")
</p>
</td>
</tr>
</table>