How do you get a client on the server's method for the DateTime parameter?



  • There's the following method on the server:

    public ContentResult GetRequestsInPeriod(DateTime? startOf, DateTime stopOf)
    {
        var now = DateTime.Now;
        var _startOf = startOf?? new DateTime(now.Year, now.Month, 1);
        var _stopOf = stopOf?? now.AddDays(1);
        var requests = _requestService.GetRequestsInPeriod(_startOf, _stopOf)
        return Content(JsonConvert.SerializeObject(requests), "application/json")
    }
    

    There is the following:

    <div ng-app="app">
        <div ng-controller="ReportController">
            <div class="container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Параметры отчета:
                </div>
                <div class="panel-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <h4>Период отчета:</h4>
                            <label>С:</label>
                            <div class="input-group col-md-4">
                                <input type="text" name="startOf" class="form-control" uib-datepicker-popup="dd.MM.yyyy" ng-model="startOf" is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
                                </span>
                            </div>
                            <label>По:</label>
                            <div class="input-group col-md-4">
                                <input type="text" name="stopOf" class="form-control" uib-datepicker-popup="dd.MM.yyyy" ng-model="stopOf" is-open="popup2.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button>
                                </span>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="panel-footer">
                    <input type="button" value="Получить отчет" ng-click="getReport(startOf, stopOf)" class="btn btn-default" />
                </div>
            </div>
        </div>
            <h3 class="text-center">Простой оборудования по заявкам за период: с {{startOf}} по {{stopOf}}</h3>
    
        &lt;h4&gt;Отчет выполнил: @Model.ReportInfo.Author&lt;/h4&gt;
        &lt;h4&gt;Дата/время получения отчета: @DateTime.Now.ToString("dd.MM.yyyy hh:mm")&lt;/h4&gt;
    
     &lt;!--Здесь таблица для отображения списка заявок--&gt;
    
    &lt;/div&gt;
    

    </div>

    js code:

    var app = angular.module('app', ['ui.bootstrap', 'ngLocale']);

    app.controller('ReportController', ['$scope', '$http', function ($scope, $http) {

    var now = new Date();
    $scope.startOf = new Date(now.getFullYear(), now.getMonth(), 1);
    $scope.stopOf = now.setDate(now.getDate() + 1);
    
    $scope.getReport = function (startOf, stopOf) {
        console.log(startOf);
        console.log(stopOf);
        var url = '/Report/GetRequestsInPeriod';
        $http.get(url)
        .success(function (data) {
            $scope.list = { requests: data };
        });    
    };
    
    $http.get('/Report/GetRequestsInPeriod')
        .success(function (data) {
            $scope.list = { requests: data };
        });
    
    $scope.dateOptions = { startingDay: 1 };
    
    $scope.open1 = function () {
        $scope.popup1.opened = true;
    };
    
    $scope.open2 = function () {
        $scope.popup2.opened = true;
    };
    
    $scope.popup1 = {
        opened: false
    };
    
    $scope.popup2 = {
        opened: false
    };    
    

    }]);

    Help implement the method getReport(startOf, stopOf) Total /questions/tagged/angularjs Controller, if more precise, how to form url to be transferred to the server in the input field(s)startOfstopOf)



  • The whole question is how to make MVC able to transfer the date transferred.

    Alternatively, a second transfer to UTC may be possible, or translation of the date into a line with such a format that the MVC converter can recognize it

    The simplest will be the use of the ISO format, it can be collected manually using the filter dateor use the function https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

    I didn't understand why the method was returning ContentResult, not JsonResult.

    Sending can look like:

    $http.get('url?question='+d.toISOString())
    

    The silver part is:

    public JsonResult GetAnswer(DateTime question)
    {               
        return Json(new {answer="answer"}, JsonRequestBehavior.AllowGet);
    }
    

    https://dotnetfiddle.net/wjUPO0




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2