2016-08-01 1 views
0

Je ne peux pas convertir la valeur d'un type d'entrée TIME en html en TimeSpan dans un contrôleur MVC avec C#. J'envoie les valeurs d'entrée avec Jquery.Conversion de l'heure d'entrée HTML en TimeSpan

<div class="row"> 
      <div class="col-md-6"> 
       <label>Hora inicio: </label><input type="time" class="form-control" ng-model="hora.inicioc" /> 
      </div> 
      <div class="col-md-6"> 
       <label>Hora fin: </label><input type="time" class="form-control" ng-model="hora.finc" /> 
      </div> 
     </div> 

Et Mon Jquery est:

$scope.AddReg = function() { 
     AddProduccion(); 
     $.ajax 
     ({ 
      type: "POST", 
      //the url where you want to sent the userName and password to 
      url: 'http://localhost:2713/Produccion/AgregarProduccion/', 
      contentType: "application/json; charset=utf-8", 
      async: true, 
      //json object to sent to the authentication url 
      data: JSON.stringify({dp : $scope.materiales, p :$scope.produccion}), 
      success: function() { 

      alert("Se agregó registro de produccion"); 
      } 
     }) 
    }; 

Et avant cela, je pousse les modèles ng à un tableau d'objets pour l'envoyer avec Jquery.

var AddProduccion = function() { 
     $scope.produccion.push(
      { 
       hora_inicio_congelacion: $scope.hora.inicioc, 
       hora_fin_congelacion: $scope.hora.finc, 
       hora_inicio_deshielo: $scope.hora.iniciod, 
       hora_fin_deshielo: $scope.hora.find, 
       hora_registro: "", 
       total_producido: 5, 
       total_merma: 5 
      } 
     ); 
    }; 

Et Mon contrôleur est gettig Deux listes d'objets.

[HttpPost] 
    public ActionResult AgregarProduccion(List<DetalleProduccionBolsasViewModel> dp, List<PBolsasModel> p) 

où mon PBolsasModel est

public class PBolsasModel 
{ 
    public System.TimeSpan hora_inicio_congelacion { get; set; } 
    public System.TimeSpan hora_fin_congelacion { get; set; } 
    public System.TimeSpan hora_inicio_deshielo { get; set; } 
    public System.TimeSpan hora_fin_deshielo { get; set; } 
    public System.DateTime hora_registro { get; set; } 
    public double total_producido { get; set; } 
    public double total_merma { get; set; } 
} 

je l'ai déjà essayé de régler le hour_inicio_congelacion, hora_fin_congelacion sous forme de chaîne, puis convertir leur mais je m'y suis une erreur parce que la valeur que je reçois de la vue pas le format pour le convertir en TimeSpan. J'ai aussi essayé de pleurer.

+1

Quelle est la valeur que vous avez dans l'une de ces propriétés (ex: hora_inicio_congelacion)? – Shyju

+0

"1970-01-01T21: 03: 00.000Z" ce format de temps – MisaelGaray

Répondre

1

Je fais comme ça

<input type="text" name="StartTime" value="20:45" /> 

C#/MVC

public TimeSpan StartTime {get; set;} 

Dans mon cas, j'utilisais timepicker bootstrap le format & est réglé sur 24 heures. Assurez-vous que vous utilisez type = "text" et que le format de l'heure est réglé sur 24 heures.