asp.net mvc 5从ajax scrypt向控制器发送两个参数

时间:2016-10-23 18:13:22

标签: asp.net ajax datetime parameters asp.net-mvc-5

在我看来,我有:

    @Html.EditorFor(model => model.From, new { htmlAttributes = new {id = "fromEdit", @class = "form -control", @style = "width:300px" } })

    @Html.EditorFor(model => model.To, new {htmlAttributes = new { id = "toEdit", @class = "form-control", @style = "width:300px"}})

我需要在一个控制器动作中调用来自scrypt的两个DatetTime参数:

    <script>
        var dataToSend = {
            startDate: $("#fromEdit").getDate,
            endDate: $("#toEdit").getDate
        }

        $(document).ready(function() {
            $('#example').DataTable({
                "ajax": {
                    "url": RootUrl + "SalesDetailsReport/GetSalesDetailReport",
                    "type": "GET",
                    "data": dataToSend ,
                    "dataType": "json"
                },
                "aoColumns": [
                    { "data": "Date", "autoWidht": true },
                    { "data": "Client", "autoWidht": true },
                    { "data": "Product", "autoWidht": true },
                    { "data": "Costs", "autoWidht": true },
                    { "data": "Profit", "autoWidht": true },
                    { "data": "ProfitMarginPercentage", "autoWidht": true },
                    { "data": "Amount", "autoWidht": true },



                ]
            });
        });
    </script>

在我的控制器中:

public ActionResult GetSalesDetailReport(DateTime pFrom , DateTime pTo)
有些事情是错误的(或者是永恒的),因为在浏览器的属性中我看到:

GetSalesDetailReport?_ = 1477245791871 500(内部服务器错误)

并且永远不会调用控制器操作。#

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

您的操作方法参数为pFrompTo。因此,请确保您发送的js对象具有相同的属性名称。

var dataToSend = {
                  pFrom: $("#fromEdit").val(),
                  pTo:  $("#toEdit").val()
                }