【问题标题】:Pass a datetime value from datepicker in View to Controller using Jquery Ajax使用 Jquery Ajax 将 View 中的 datepicker 中的 datetime 值传递给 Controller
【发布时间】:2018-04-18 09:30:02
【问题描述】:

在我看来,我有这个代码:

<input type="text" name="date" class="datepicker" id="calendar" />
<script type="text/javascript">
 $("#calendar")
        .datepicker({ dateFormat: 'dd/mm/yy' })
        .on("changeDate", function (e) {
            $.ajax({
                type: "GET",
                url: '@Url.Action("Index","Home")' + '?date=' + e.date.toISOString(),
                success: function (data) {
                    if (data.success) {
                        alert(e.date)
                    }
                }
            });
    });

    $("#calendar").datepicker('setDate', 'today');

</script>

当我在 Datepicker 中更改日期时,我希望 Ajax 调用 Index 方法:

     [HttpGet]
        public ActionResult Index(string date)
        {if (date == null) { ViewBag.dt = 1; };
//code
}

我尝试了很多建议,但在我看来总是得到 1 意味着没有值传递给控制器​​,请帮助。

【问题讨论】:

标签: javascript jquery ajax asp.net-mvc


【解决方案1】:

您似乎在 url 参数中使用了错误的 URL 格式。您需要在 AJAX 调用中将 JS 端的日期时间值作为 data 参数传递,并使用 Nullable&lt;DateTime&gt; 作为操作方法参数:

JS

$.ajax({
    type: "GET",
    url: '@Url.Action("Index", "Home")',
    data: { date: e.date.toISOString() },
    success: function (data) {
        if (data.success) {
            // do something
        }
    }
});

控制器

[HttpGet]
public ActionResult Index(DateTime? date)
{
    // other stuff
}

编辑 1:

我发现你可以使用"getDate" 参数传递datepicker 值,这样你的日期选择器代码就变成了这样:

$("#calendar").datepicker({ dateFormat: 'dd/mm/yy' })
              .on("changeDate", function() {
                  // get date value
                  var datetime = $("#calendar").datepicker("getDate").toISOString();

                  $.ajax({
                      type: "GET",
                      url: '@Url.Action("Index", "Home")',
                      data: { date: datetime },
                      success: function (data) {
                      if (data.success) {
                          // do something
                      }
                  }
              });
});

【讨论】:

  • OP 的代码为 url 产生完全相同的结果(尽管你的建议是更好的方法)
  • 在控制器中,参数应该可以为空public ActionResult Index(DateTime? date==Null),对吧? (因为我将它作为 Datetime 传递,但出现错误。
  • 带调试器:“datetime is not defined”和“eval at anonymous (:1:1)”是什么意思?
  • 尝试将datetime 声明放在changeDate 事件之外,你会得到什么?或者直接在控制台使用$("#calendar").datepicker("getDate").toISOString();有什么价值?
  • 总是相同的消息“...未定义”
【解决方案2】:

试试这个,它对我有用

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <input type="text" name="date" class="datepicker" id="calendar" onblur="DemoEvent();"/>
        </div>
    </div>
</div>
@section scripts{
<script type="text/javascript">


      function DemoEvent() {
        alert();
            $.ajax({
                type: "GET",
                url: '@Url.Action("Index","Home")' + '?date=' + $("#calendar").val(),
                success: function (data) {
                    if (data.success) {
                        alert(e.date)
                    }
                }
            });
    }

    //$("#calendar").datetimepicker('setDate', 'today');
//
    window.onload = function () {
        $('.datepicker').datetimepicker({})
    }

</script>
    }

【讨论】:

  • 数据选择器是否加载到文本框中?
  • 不,它不仅仅是一个空警报。
猜你喜欢
  • 1970-01-01
  • 2017-12-26
  • 2023-01-24
  • 2016-05-27
  • 2018-09-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2020-08-23
相关资源
最近更新 更多