将HTML输入时间转换为TimeSpan

本文关键字:TimeSpan 转换 时间 HTML 输入 | 更新日期: 2023-09-27 17:59:27

我无法在带有C#的MVC控制器中将html中的输入类型TIME的值转换为TimeSpan。我用Jquery发送输入值。

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

我的控制器正在获取两个对象列表。

[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_icio_congelacion、hora_fin_congelacion设置为字符串,然后转换它们,但我遇到了一个错误,因为我从视图中获得的值没有将其转换为TimeSpan的格式。我也试着哭。

将HTML输入时间转换为TimeSpan

我喜欢这个

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

C#/MVC

public TimeSpan StartTime {get; set;}

在我的案例中,我使用了bootstrap时间选择器&格式设置为24小时。请确保您使用的是type="text",并且时间格式设置为24小时。