jeudi 10 décembre 2015

ASP.NET MVC 3 Razor DateTime.Parse don't work

I'm trying to do a cast with dates but it's throws me an exception .

The code is:

This Works ->

var FechaInicio = Model != null ? DateTime.Parse(Model.FechaInicio).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");

This Doesn't Work ->

var FechaFin = Model != null ? DateTime.Parse(Model.FechaFin).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");

The model is Ok and values are dates in string format

The error is:

Server Error in '/' Application. String was not recognized as a valid DateTime. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:

Line 13:    var Ubicacion = Model != null ? Model.Ubicacion : null;
Line 14:    var FechaInicio = Model != null ? DateTime.Parse(Model.FechaInicio).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");
Line 15:    var FechaFin = Model != null ? DateTime.Parse(Model.FechaFin).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");

Aucun commentaire:

Enregistrer un commentaire