Karen Wiznia Karen Wiznia - 6 months ago 16
Javascript Question

Send a date to an action in javascript

I have a datepicker and I need to get a date and pass that date to the controller. So far I have the following code:

JavaScript function:

$(function () {
intDate = Date;
$("#datepicker").datepicker({
onClose: function (select_date) {
//console.log(select_date);
var date = $('#datepicker').val();
console.log(date.toString());
$.ajax('/Home/GetUserInfoDate', {
data: {
intDate: date
},
success: function (data, textStatus, jqXHR) {
//this will happen on success of request
$('#divData').html(data);
},
error: function () {
console.log("error handler when ajax request fails... ");
},

});
//console.log(date);
}
});
});


Model:

public IEnumerable<DateTime> getInfoByDate(DateTime date)
{
CareDB context = new CareDB();

SqlParameter Date = new SqlParameter("@Date", date);

object[] parameters = new object[] { Date };

IEnumerable<DateTime> lst = context.ReleaseDate.SqlQuery("_UserInformationByDate @Date", parameters).ToList();

context.Dispose();
context = null;
return lst;
}


Controller:

public ActionResult EmployeeDate(MvcApplication1.Models.DateTime date)
{
Models.BL oBL = new Models.BL();

IEnumerable<MvcApplication1.Models.DateTime> lstEmployees = oBL.getInfoByDate(date);

ViewBag.DataSource = lstEmployees;

return View("EmployeeInformation");
}


When I try to execute the function I get this message:


jquery-1.10.2.js:8706 GET http://localhost:51299/Home/GetUserInfoDate?intDate=05%2F03%2F2016 404 (Not Found)


And then of course say that it was an error.

I think the problem is with the format of the date, because as you can see it has a weird format.. Any ideas?

Any help will be appreciated! thanks!

hsh hsh
Answer

you are sending request to GetUserInfoDate however your controller name is getInfoByDate you need to change your code like this:

$(function () {
  intDate = Date; 
  $("#datepicker").datepicker({
      onClose: function (select_date) {
          //console.log(select_date);
          var date = $('#datepicker').val();
          console.log(date.toString());
          $.ajax('/Home/getInfoByDate', {
              data: {
                  date: date
              },
              success: function (data, textStatus, jqXHR) {
                  //this will happen on success of request
                  $('#divData').html(data);
              },
              error: function () {
              console.log("error handler when ajax request fails... ");
          },

          });
          //console.log(date);
          }
  });

});