所以我在控制器中有这段代码,可以通过雇员的ID查看特定雇员的假期清单
public JsonResult GetVacatioinList(int? Id)
{
db.Configuration.ProxyCreationEnabled = false;
List<Vacations> VList = db.Vacations.Include(c => c.VacationType).Select(x => new Vacations
{
EmployeesId = x.EmployeesId,
Id = x.Id,
VacationType = x.VacationType,
StartDate = x.StartDate,
EndDate = x.EndDate,
Duration = x.Duration
}).Where(x=> x.EmployeesId == Id).ToList();
return Json(VList, JsonRequestBehavior.AllowGet);
}
,此按钮将在URL中显示特定员工的ID
<a class='btn btn-warning' href='http://localhost:26868/Vacations/Index?id='" + EmployeesList[i].Id + "><span class='glyphicon glyphicon-list'></span></a>
现在我需要将URL的ID与该用户的数据库中的ID进行比较(如果两者都显示该员工的假期)
我尝试了这个,当我没有在控制器的LINQ语句中添加where条件但没有显示数据的地方时,它显示了所有假期 这是我使用的jQuery代码
<script>
$("#LoadingStatus").html("Loading....");
$.get("/Vacations/GetVacatioinList", null, DataBind);
function DataBind(VacatioinList) {
var SetData = $("#SetVacatioinList");
for (var i = 0; i < VacatioinList.length; i++) {
if (VacatioinList[i].EmployeesId = Id) {
var dateforstart = new Date(parseInt(VacatioinList[i].StartDate.substr(6)));
var startdate = "";
startdate += dateforstart.format("mmm d, yyyy");
var dateforend = new Date(parseInt(VacatioinList[i].EndDate.substr(6)));
var enddate = "";
enddate += dateforend.format("mmm d,yyyy");
var Data = "<tr class='row_" + VacatioinList[i].Id + "'>" +
"<td>" + VacatioinList[i].Id + "</td>" +
"<td>" + VacatioinList[i].VacationType.Name + "</td>" +
"<td>" + VacatioinList[i].Duration + "</td>" +
"<td>" + startdate + "</td>" +
"<td>" + enddate + "</td>" + "</tr>";
SetData.append(Data);
$("#LoadingStatus").html("");
}
}
}
有关如何解决它的任何建议或实现该问题的替代方法
答案 0 :(得分:0)
如果我理解正确,那么您编写的js代码将存在:
http://localhost:26868/Vacations/Index?id=someId
例如:
http://localhost:26868/Vacations/Index?id=55
如果是这种情况,则此行代码:
$.get("/Vacations/GetVacatioinList", null, DataBind);
正在调用“ / Vacations / GetVacatioinList”操作,而未传递Id参数。
您的MVC动作收到的ID值为0,因此您的数据库没有返回任何结果。
您需要:
更改您的$ get行
$.get("/Vacations/GetVacatioinList", { Id: getParameterByName('id') }, DataBind);
然后您可以删除
if (VacatioinList[i].EmployeesId = Id)
最后,为了改善数据库调用,请将“位置”部分移到“选择”部分之前
db.Vacations.Include(c => c.VacationType)
.Where(x=> x.EmployeesId == Id)
.Select(x => new Vacations
{
EmployeesId = x.EmployeesId,
Id = x.Id,
VacationType = x.VacationType,
StartDate = x.StartDate,
EndDate = x.EndDate,
Duration = x.Duration
})
.ToList();