A couple things, if you have a repository model, don't put a DbContext on your controller (you're wasting cycles if you do). We had a misunderstanding about where code lay in specific places, so I'm trying to be more specific here.
I'm going to go ahead, build and test, but this should be a total solution for this module, which is normally a bit more than folks are looking at in quick answers.
I've run and validated the code, it works just fine as is.
AddressModel.cs
public class AddressModel
{
public int CountryId { get; set; }
[Display(Name = "Country")]
public IList<SelectListItem> AvailableCountries { get; set; }
[Display(Name = "State")]
public int StateId { get; set; }
public IList partialModel { get; set; }
public AddressModel()
{
AvailableCountries = new List<SelectListItem>();
}
}
AddressRepository.cs
public class AddressRepository
{
public Entities db = new Entities();
public IList<Country> GetAllCountries()
{
return db.Countries.ToList();
;
}
public IList<State> GetAllStatesByCountryId(int countryId)
{
return db.States.Where(x => x.Country_Id == countryId).ToList();
}
}
AddressController.cs
public class AddressController : Controller
{
private AddressRepository _repository1 { get; set; }
public AddressController()
: this(new AddressRepository())
{
}
public AddressController(AddressRepository repository)
{
_repository1 = repository;
}
public ActionResult Index()
{
var model = new AddressModel();
model.AvailableCountries.Add(new SelectListItem
{
Text = "-Please select-",
Value = "Selects items"
});
var countries1 = _repository1.GetAllCountries();
foreach (var country in countries1)
{
model.AvailableCountries.Add(new SelectListItem()
{
Text = country.Name,
Value = country.Id.ToString()
});
}
return View(model);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetStates(int countryId)
{
if (countryId == 0)
{
throw new ArgumentException("countryId");
}
return PartialView(_repository1.GetAllStatesByCountryId(countryId));
}
}
index.cshtml
@model FarControl.Models.AddressModel
@{
ViewBag.Title = "Index";
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#CountryId').change(function () {
var selectedItem = $(this).val();
var statesProgress = $('#states-loading-progress');
statesProgress.show();
$.ajax({
cache: false,
type: 'GET',
url: '@(Url.Action("GetStates"))',
data: { 'countryId': selectedItem },
success: function (data) {
$('#tableStates').html(data);
statesProgress.hide();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
statesProgress.hide();
}
});
});
});
</script>
@Html.LabelFor(model => model.CountryId)
@Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries)
<br />
<div id="tableStates"></div>
<span id="states-loading-progress" style="display: none;">Please wait..</span>
GetStates.cshtml
@model IEnumerable<State>
@if (Model != null)
{
<table>
@foreach (var state in Model)
{
<tr><td>@state.Name</td></tr>
}
</table>
}