我目前拥有一个使用Entity Framework 6(ASP.NET MVC 5)从现有数据库生成的数据模型。我目前正在使用两个数据表:合同和员工。它们都与Company表相关,因此每个表都将CompanyID作为外键。当我搭建“创建合同”视图时,它当前具有EmployeeID和CompanyID的下拉列表。我要做的是让用户选择CompanyID并仅显示与该公司关联的员工。
我尝试过使用jQuery的getJSON方法,但由于循环引用而返回500错误,因为我正在返回一个JSON序列化的Employee对象,该对象具有对Company的引用(导致循环引用错误)。
员工模型:
public partial class Employee
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Employee()
{
this.Contracts = new HashSet<Contract>();
this.HoleLoggings = new HashSet<HoleLogging>();
}
public int EmployeeID { get; set; }
public int CompanyID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public virtual Company Company { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Contract> Contracts { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HoleLogging> HoleLoggings { get; set; }
}
合同模型:
public partial class Contract
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Contract()
{
this.ContractDetails = new HashSet<ContractDetail>();
this.Platforms = new HashSet<Platform>();
this.ShiftReports = new HashSet<ShiftReport>();
}
public int ContractID { get; set; }
public int EmployeeID { get; set; }
public int CompanyID { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public Nullable<bool> IsApproved { get; set; }
public virtual Company Company { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ContractDetail> ContractDetails { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Platform> Platforms { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ShiftReport> ShiftReports { get; set; }
public virtual Employee Employee { get; set; }
}
这是我的合约控制器中的ActionMethod:
public JsonResult GetEmployees(int id)
{
List<Employee> employees = new List<Employee>();
var employeeList = from e in db.Employees
where (e.CompanyID == id)
select e.
employees.AddRange(employeeList);
return Json(employees, JsonRequestBehavior.AllowGet);
}
视图的形式:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Contract</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CompanyID, "CompanyID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CompanyID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CompanyID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeID, "EmployeeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EmployeeID", new SelectList(string.Empty, "Value", "Text"), "Please select an Employee", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
和jQuery:
$(function () {
$("#CompanyID").change(function () {
$("#EmployeeID").empty();
var token = $('[name=__RequestVerificationToken]').val();
$.getJSON('@Url.Action("GetEmployees")', { id: $(this).val() }, function (employees) {
var employeesSelect = $("#EmployeeID");
$.each(employees, function (i, employee) {
employeesSelect.append($('<option/>', {
value: employee.value,
text: employee.text
}));
});
});
});
});
任何建议都将不胜感激!