There is 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 view:

 <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> <h4>Отчет выполнил: @Model.ReportInfo.Author</h4> <h4>Дата/время получения отчета: @DateTime.Now.ToString("dd.MM.yyyy hh:mm")</h4> <!--Здесь таблица для отображения списка заявок--> </div> </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 to implement the getReport method (startOf, stopOf) in the controller, to be more precise how to form the url that the entered values ​​would be transferred to the server in the input field ( startOf , stopOf ).

  • I don’t see something to be transmitted somewhere - Grundy
  • @Grundy if you mean that there are no parameters in the url that is specified in the getReport() method, then this is the problem, if the parameter were of type int I would do so "/Report/Requests?parameterId="+id ; but I don’t understand how to do it with DateTime at the support, like this: "/Report/Requests?startOf=+startOf+"&stopOf="+stopOf ; null in the parameters comes to the server - Bald
  • for each of the methods in $http second parameter is the config :) in which you can set the data field to be transmitted. - Grundy
  • one
    the server receives null in the parameters - this is because mvc cannot convert the received strings into a date. So that he could just go in ISO format lines like this: "yyyy-MM-dd HH: mm: ss" - Grundy
  • Writing `You 'with a capital letter is cool) - Vladimir Gamalyan

1 answer 1

You can transfer the date in any way you like; the whole question is how to make MVC able to translate the transferred string to its date.

As options, it can be the transfer of seconds in UTC, or the translation of the date into a string with such a format so that the MVC converter can recognize it

The easiest way is to use the ISO format, you can assemble it manually using the date filter, or use the toISOString function

I did not quite understand why the method returns ContentResult, not JsonResult

Sending may look like this:

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

Server part as follows:

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

Dotnetfiddle example

  • I didn’t quite understand why the method returns ContentResult, not JsonResult because the result is a DateTime type field that serializes correctly (although maybe I just don’t know how to prepare a standard mechanism) using the JsonConvert.SerializeObject nuget method of the nuget package - Bald