2016-08-01 61 views
0

我无法使用C#将HTML中的输入类型TIME的值转换为MVC控制器中的TimeSpan。 我使用Jquery发送输入值。将HTML输入时间转换为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> 

和我的jQuery是:

$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"); 
      } 
     }) 
    }; 

而做到这一点之前,我推了NG-模型对象的数组来用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 
      } 
     ); 
    }; 

而我的控制器是gettig两个对象列表。

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

在我的PBolsasModel是

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; } 
} 

我已经尝试设置hour_inicio_congelacion,hora_fin_congelacion为字符串,然后将它们的,但我得到一个错误,因为我从视图中所获得的价值都没有将其转换为TimeSpan的格式。我也想哭了。

+1

你在其中一个属性中有什么价值(例如:hora_inicio_congelacion)? – Shyju

+0

“1970-01-01T21:03:00.000Z”这种时间格式 – MisaelGaray

回答

1

我不喜欢这样

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

C#/ MVC

public TimeSpan StartTime {get; set;} 

在我来说,我用的是引导timepicker &格式设置为24小时。确保你使用的是type =“text”,时间格式设置为24Hrs。

相关问题