MVC使用javascript将日期范围传递给控制器

时间:2013-04-26 16:45:18

标签: javascript asp.net-mvc

很抱歉,如果我的问题听起来像菜鸟,但我是javascript的真正初学者。我有2个参数“ startdate ”和“ enddate ”。我想通过这个javascript传递给控制器​​:

查看:(已更新)

<div class="inventoryTypeList">
<p>
    <label for="start">From:</label>
        @(Html.Kendo().DatePicker()
              .Name("from")
              .Events(e => e.Change("startChange"))
        )

        <label for="end">To:</label>
        @(Html.Kendo().DatePicker()
              .Name("to")
              .Events(e => e.Change("endChange"))
        )
</p>
    <button class="k-button" id="showGrid">Search</button>
</div>

<script>

    function startChange() {
        var endPicker = $("#to").data("kendoDatePicker"),
            startDate = this.value();

        if (startDate) {
            startDate = new Date(startDate);
            startDate.setDate(startDate.getDate() + 1);
            endPicker.min(startDate);
        }
    }

    function endChange() {
        var startPicker = $("#from").data("kendoDatePicker"),
            endDate = this.value();

        if (endDate) {
            endDate = new Date(endDate);
            endDate.setDate(endDate.getDate() - 1);
            startPicker.max(endDate);
        }
    }

    $(function () {

        $("#showGrid").click(function () {
            $.ajax({
                url: '@Url.Action("PastTransactionsPartial", "InventoryTransactions")',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                cache: false,
                data: JSON.stringify({
                    dateFrom: $('#from').val(),
                    dateTo: $('#to').val(),
                }),
                success: function (result) {
                    $('#divPastTransactions').replaceWith(result);
                },
                error: function () { alert("Please specify from and to dates"); }
            });
        });
    });

</script>

<div id="divPastTransactions"  ></div>

控制器:(已更新)

public ActionResult PastTransactionsPartial(DateTime dateFrom, DateTime dateTo)
    {
        var inventorytransactions = db.InventoryTransactions.Include(i => i.InventoryTransactionType).Include(i => i.InventoryType).Include(i => i.Supplier).Where(i => i.InventoryTransactionDate >= dateFrom && i.InventoryTransactionDate <= dateTo);
        return PartialView("_PastTransactionsPartial", inventorytransactions.ToList());
    }

我该怎么做?我在控制器中捕获它们是什么?

结果将显示在搜索功能

下的部分视图中

编辑: 关闭此线程并将问题放入新线程

1 个答案:

答案 0 :(得分:1)

查看:

<div class="searchPastTransactions">
<p>
    <label for="start">From:</label>
        @(Html.Kendo().DatePicker()
              .Name("from")
              .Events(e => e.Change("startChange"))
        )

        <label for="end">To:</label>
        @(Html.Kendo().DatePicker()
              .Name("to")
              .Events(e => e.Change("endChange"))
        )
</p>
    <button class="k-button" id="showGrid">Search</button>
</div>

<script>

    function startChange() {
        var endPicker = $("#to").data("kendoDatePicker"),
            startDate = this.value();

        if (startDate) {
            startDate = new Date(startDate);
            startDate.setDate(startDate.getDate() + 1);
            endPicker.min(startDate);
        }
    }

    function endChange() {
        var startPicker = $("#from").data("kendoDatePicker"),
            endDate = this.value();

        if (endDate) {
            endDate = new Date(endDate);
            endDate.setDate(endDate.getDate() - 1);
            startPicker.max(endDate);
        }
    }

    $(function () {

        $("#showGrid").click(function () {
            $.ajax({
                url: '@Url.Action("PastTransactionsPartial", "InventoryTransactions")',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                cache: false,
                data: JSON.stringify({
                    dateFrom: $('#from').val(),
                    dateTo: $('#to').val(),
                }),
                success: function (result) {
                    $('#divPastTransactions').replaceWith(result);
                },
                error: function () { alert("Please specify from and to dates"); }
            });
        });
    });

</script>

<div id="divPastTransactions"  ></div>

控制器:

public ActionResult PastTransactionsPartial(DateTime dateFrom, DateTime dateTo)
        {
            var inventorytransactions = db.InventoryTransactions.Include(i => i.InventoryTransactionType).Include(i => i.InventoryType).Include(i => i.Supplier).Where(i => i.InventoryTransactionDate >= dateFrom && i.InventoryTransactionDate <= dateTo);
            return PartialView("_PastTransactionsPartial", inventorytransactions.ToList());
        }