Оказывается, у меня есть Jquery из выпадающего списка в casacade, он содержит данные из округов и служб, и я хочу, чтобы при входе пользователя отображались только данные района, из которого пользователь, пользователь имеетназначенный район.
Это мой код на контроллере:
public JsonResult GetServices(int districtId)
{
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services.Where(s => s.DistrictId == districtId).OrderBy(s => s.Name);
return Json(services);
}
Это мой сценарий Jquery:
<script type="text/javascript">
$(document).ready(function () {
$("#DistrictId").change(function () {
$("#ServiceId").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetServices")',
dataType: 'json',
data: { districtId: $("#DistrictId").val() },
success: function (districts) {
$.each(districts, function (i, service) {
$("#ServiceId").append('<option value="'
+ service.ServiceId + '">'
+ service.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve services.' + ex);
}
});
return false;
})
});
</script>
Мой вид:
<div class="form-group">
@Html.LabelFor(model => model.DistrictId, "District", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ServiceId, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ServiceId, "", new { @class = "text-danger" })
</div>
</div>