1, view page(jquery)
@{
ViewBag.Title = "Home Page";
}
<h3>New Findahome Test</h3>
<ul>
<li>
<select name="pmarket" id="pmarket">
<option value="-1">---Any---</option>
@foreach (var market in Mvc4Findahome.Helpers.Cache.PropMarkets)
{
<option value="@market.idPropMarket">@market.MarketName</option>
}
</select>
</li>
<li>
<select name="ptype" id="ptype">
<option value="-1">---Any---</option>
@foreach (var type in Mvc4Findahome.Helpers.Cache.PropTypes)
{
<option value="@type.idPType">@type.PTypeName</option>
}
</select>
</li>
<li>
<select name="cc" id="cc">
<option value="-1">---Any---</option>
@foreach (var cc in Mvc4Findahome.Helpers.Cache.Counties)
{
<option value="@cc.idCountyCity">@cc.CountyCityName</option>
}
</select>
</li>
<li>
<select id="dd" name="dd">
<option value="-1">---Any---</option>
</select>
</li>
</ul>
@* @Html.Partial("Filter", new Mvc4Findahome.Models.PropertyModel.Filter())*@
<script type="text/javascript">
$(function () {
$("select#cc").change(function (evt) {
if ($("select#cc").val() != "-1") {
$.ajax({
url: "/Home/location",
type: 'POST',
data: { OS: $("select#cc").val() },
success: function (response) {
// $("select#dd").replaceWith(resonse)
$("select[id$=dd] option").remove(); //remove old option
$("select[id$=dd]").append("<option value='-1'>--Any--</option>"); // add default
$("select[id$=dd]").append(response) // add new options
},
error: function (xhr) {
alert("There are something wrong");
}
});
}
});
});
</script>
2, Controller
[HttpPost]
public String location(int OS)
{
var ds = Cache.Districts.Where(c=>c.idCountyCity == OS).OrderBy(c=>c.Name).ToList();
String ReturnList = "";
foreach (var d in ds)
{
ReturnList += String.Format("<option value='{0}'>{1}</option>", d.idDistrict.ToString(), d.Name);
}
return ReturnList;
}