0

so i have this code in the controller to view a list of vecation to specfic employee by his 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);
    }

and this button will show the id for specific employee in URL

     <a  class='btn btn-warning' href='http://localhost:26868/Vacations/Index?id='" + EmployeesList[i].Id + "><span class='glyphicon glyphicon-list'></span></a>

now i need to compare the ID coming from URL with one in database for that user if both the same show the vacations for that employee

i tried this and it shows all the vacation when i didn't add the where condition in the LINQ statement in controller but with where there is no data shown this is my jquery code that i used

    <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("");
        }
    }

}

any recommendation on how to fix it or an alternative way to implement that

Osama
  • 3
  • 1

1 Answers1

0

If i understood you right, then the js code you wrote is living at:
http://localhost:26868/Vacations/Index?id=someId
for example: http://localhost:26868/Vacations/Index?id=55

if this is the case then this line of code:

$.get("/Vacations/GetVacatioinList", null, DataBind);

is calling "/Vacations/GetVacatioinList" action without passing the Id parameter.
your MVC Action receives 0 as the Id value so no results returned from your db.

you need to:

  1. make sure you actually have an id value in the querystring (Vacations/Index?id=someId)
  2. read that value with js (you can use this helper function in order to get it)
  3. change your $get line

    $.get("/Vacations/GetVacatioinList", { Id: getParameterByName('id') }, DataBind);
    

then you can remove the

if (VacatioinList[i].EmployeesId = Id) 

finally, in order to improve a bit the db call, move the "Where" part before the "Select" part

 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();