I have these models in my users, febriles, districts and services, users and febriles data is required for districts and services, I have a dropdownlist with the following jquery and json script:
@Scripts.Render("~/bundles/jqueryval")
<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>
In the febrile driver I have this method to get the services of the jquery:
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);
}
And in my view create of febriles I have this:
@model Fevers.Models.Fever
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Fever</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<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>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuantityPatients, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QuantityPatients, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QuantityPatients, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuantityFevers, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QuantityFevers, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QuantityFevers, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
What I want to do is that after entering a user and it is logged in, only the services of the district to which the user belongs are loaded.