【问题标题】:How to pass date value to controller? asp.net mvc如何将日期值传递给控制器​​? asp.net mvc
【发布时间】:2015-07-09 22:48:23
【问题描述】:

这是 html 编码和 javascript 代码。 我正在尝试将日期值传递给控制器​​,但不知道该怎么做,因为我对 c# 和 asp.net 还是新手。我需要一些帮助和例子来做这件事

这是我需要将数据传递给控制器​​的 li list 示例。

http://cssdeck.com/labs/li7ltd8g

                        function getDates(daterangetype) {
                            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
                            var dates = {};
                            var today = new Date();

                            if (daterangetype === 'today') {
                                dates.todate = null;
                                dates.fromdate = today.getDate() + ' ' + months[today.getMonth()] + ' ' + today.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'yesterday') {
                                var fromdate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
                                dates.todate = null;

                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'last7Days') {
                                var todate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
                                var fromdate = new Date(todate.getFullYear(), todate.getMonth(), todate.getDate() - 6);

                                dates.todate = todate.getDate() + ' ' + months[todate.getMonth()] + ' ' + todate.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'last14Days') {
                                var todate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
                                var fromdate = new Date(todate.getFullYear(), todate.getMonth(), todate.getDate() - 13);

                                dates.todate = todate.getDate() + ' ' + months[todate.getMonth()] + ' ' + todate.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'last30Days') {
                                var todate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
                                var fromdate = new Date(todate.getFullYear(), todate.getMonth(), todate.getDate() - 29);

                                dates.todate = todate.getDate() + ' ' + months[todate.getMonth()] + ' ' + todate.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'allTime') {
                                var fromdate = new Date(2012, 0, 1);

                                dates.todate = today.getDate() + ' ' + months[today.getMonth()] + ' ' + today.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'thisMonth') {
                                var fromdate = new Date(today.getFullYear(), today.getMonth(), 1);

                                dates.todate = today.getDate() + ' ' + months[today.getMonth()] + ' ' + today.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'lastMonth') {
                                var fromdate = new Date(today.getFullYear(), today.getMonth() - 1, 1);

                                var temp = new Date(today.getFullYear(), today.getMonth(), 1);
                                var todate = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() - 1);

                                dates.todate = todate.getDate() + ' ' + months[todate.getMonth()] + ' ' + todate.getFullYear();
                                dates.fromdate = fromdate.getDate() + ' ' + months[fromdate.getMonth()] + ' ' + fromdate.getFullYear();

                                return dates;
                            }

                            if (daterangetype === 'thisWeek') {
                                if (today.getDay() == 0) {
                                    var temp = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 6);
                                } else {
                                    var temp = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 1);
                                }

                                dates.fromdate = temp.getDate() + ' ' + months[temp.getMonth()] + ' ' + temp.getFullYear();
                                dates.todate = today.getDate() + ' ' + months[today.getMonth()] + ' ' + today.getFullYear();

                                return dates;
                            }


                            if (daterangetype === 'lastWeek') {
                                if (today.getDay() == 0) {
                                    var temp1 = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
                                } else {
                                    var temp1 = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
                                }

                                var temp2 = new Date(temp1.getFullYear(), temp1.getMonth(), temp1.getDate() - 6);

                                dates.fromdate = temp2.getDate() + ' ' + months[temp2.getMonth()] + ' ' + temp2.getFullYear();
                                dates.todate = temp1.getDate() + ' ' + months[temp1.getMonth()] + ' ' + temp1.getFullYear();

                                return dates;
                            }
                            if (daterangetype === 'custom') {
                                var temp1 = $("#todate").datepicker('getDate');
                                var temp2 = $("#fromdate").datepicker('getDate');

                                dates.fromdate = temp2.getDate() + ' ' + months[temp2.getMonth()] + ' ' + temp2.getFullYear();
                                dates.todate = temp1.getDate() + ' ' + months[temp1.getMonth()] + ' ' + temp1.getFullYear();

                                return dates;
                            }
                        }

如何将日期参数传递给控制器​​?我真的不知道。不过可能需要帮助。

 <div style="float:right; margin-right:200px; width:60px;margin-top:0px; z-index:100; padding-bottom:2px;">
                               <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                               <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
                               <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
                           <body>
                               <div id="datebox" class="wrapper-dropdown" tabindex="1">
                                   <div id="select-container">
                                       <span class="daterangetype">Custom</span>
                                       <span class="daterange">            
                                               <span class="fromdate">
                                                   25 Apr 2015
                                               </span>                                                                                         
                                           </span>
                                           <span class="todate">
                                               - 28 Apr 2015
                                           </span>
                                   </div>
                                   <ul class="dropdown">
                                       <li data-daterangetype="custom" id="custom">
                                           <a href="#">Custom</a>
                                           <span class="datepicker-wrapper">
                                               <input type="text" id="fromdate" class="datepicker" /> -
                                               <input type="text" id="todate" class="datepicker" />
                                           </span>
                                       </li>
                                       <li data-daterangetype="today" selected="selected"><a href="#">Today</a></li>
                                       <li data-daterangetype="yesterday"><a href="#">Yesterday</a></li>
                                       <li data-daterangetype="thisWeek"><a href="#">This Week</a></li>
                                       <li data-daterangetype="last7Days"><a href="#">Last 7 Days</a></li>
                                       <li data-daterangetype="lastWeek"><a href="#">Last Week</a></li>
                                       <li data-daterangetype="thisMonth"><a href="#">This Month</a></li>
                                       <li data-daterangetype="last30Days"><a href="#">Last 30 Days</a></li>
                                       <li data-daterangetype="lastMonth"><a href="#">Last Month</a></li>
                                       <li data-daterangetype="allTime"><a href="#">All Time</a></li>
                                       <li><button class="button" id="apply">Apply</button></li>
                                   </ul>
                               </div>
                           </body>
                           </div>

【问题讨论】:

  • 您可以使用 ajax 来做到这一点,例如在 jQuery 的帮助下

标签: c# asp.net-mvc


【解决方案1】:

使用 JavaScript 方法.toUTCString()

控制器示例:

public ActionResult GetMyTime(DateTime myTime)
{
    return View();
}

一个视图示例:

$.post('/myController/GetMyTime', { myTime: myJSTimeVar.toUTCString()
}, function (result) {
    callback(result); 
});

【讨论】:

    【解决方案2】:

    您可以使用 jquery-ajax 功能。通过在 javascript 中通过 ajax 调用 action 方法创建一个函数,并发送获取 action 方法的参数。

    这里是示例代码。

    function AjaxCall(localdatevariable) {
        $.ajax({
            url: '@Url.Action("HomeController","test")',
            data: {dateparm: localdatevariable},
            type: 'POST',
            success: function(data) {
                process(data);
            }
        });
    }
    

    并在控制器中创建一个方法来获取此请求。

    [HttpPost]
    public ActionResult test(string dateparm)
    {
        return Content("It works");
    }
    

    我认为你也学习了 Jquery/javascript 以在 MVC 上工作。

    http://www.codeproject.com/Articles/41828/JQuery-AJAX-with-ASP-NET-MVC

    Make an AJAX request using $.ajax in MVC 4

    Making a Simple Ajax call to controller in asp.net mvc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2011-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多