I've made a simple web application and it's work correctly on local db. After publishing, there is an error connected with DateTime:
System.FormatException: String was not recognized as a valid DateTime.
The code in ViewModel looks like this:
[Required]
[ValidTime]
[Display(Name = "Game time")]
public string Time { get; set; }
[Required]
[FutureDate]
[Display(Name = "Game date")]
public string Date { get; set; }
public DateTime GetDateTime()
{
return DateTime.Parse(string.Format("{0} {1}", Date, Time));
}
Controller code:
public ActionResult Create(GameFormViewModel viewModel)
{
if (!ModelState.IsValid)
return View("GameForm", viewModel);
var userId = User.Identity.GetUserId();
var game = new Game
{
TeamA = viewModel.TeamA,
TeamB = viewModel.TeamB,
DateTime = viewModel.GetDateTime(),
LeagueId = viewModel.Id,
AdminId = userId
};
_context.Games.Add(game);
_context.SaveChanges();
return RedirectToAction("MyLeagues", "Leagues");
}
Why it's work at local db and why there is an error after publishing ?
Update: Thats my FutureDate class
public class FutureDate : ValidationAttribute
{
public override bool IsValid(object value)
{
DateTime datetime;
var isvalid = DateTime.TryParseExact(Convert.ToString(value),
"dd-mm-yyyy",
CultureInfo.CurrentCulture,
DateTimeStyles.None,
out datetime);
return (isvalid && datetime >= DateTime.Now.AddDays(-1));
}
}
DateTime = viewModel.GetDateTime(),, and also add a watcher to viewModel to check the values, better yet, post the screenshot...