【问题标题】:Send a date to an action in javascript将日期发送到 javascript 中的操作
【发布时间】:2016-05-31 07:27:46
【问题描述】:

我有一个日期选择器,我需要获取一个日期并将该日期传递给控制器​​。到目前为止,我有以下代码:

JavaScript 函数:

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

型号:

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;
    }

控制器:

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

当我尝试执行该功能时,我收到以下消息:

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

然后当然会说这是一个错误。

我认为问题出在日期的格式上,因为您可以看到它的格式很奇怪。有什么想法吗?

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 日期格式看起来没有问题。 404 表示未找到请求的http://localhost:51299/Home/GetUserInfoDate 位置。

标签: javascript c# datepicker


【解决方案1】:

您正在向GetUserInfoDate 发送请求,但是您的控制器名称是getInfoByDate,您需要像这样更改您的代码:

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

});

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多