I have an ASP.NET Core application and can not seem to find a solution to my date format problem. My application is published on a Microsoft Azure server, which is probably in the US (although it appears in South of Brazil) and has caused me these problems.
I have another application in MVC5 that is also published in Azure and that does not give me the problem, I simply defined the culture in web.config and the problem of the date was solved.
<system.web>
<globalization culture="pt-BR" />
<compilation debug="true" targetFramework="4.5" />
</system.web>
In ASP.NET Core, I found some posts about the definition of culture in startup.cs, but I've tried some things without success. Has anyone experienced this and knows how I solve my problem?
Something I've tried:
public void ConfigureServices(IServiceCollection services)
{
//Setting Culture
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization();
//services.AddScoped<LanguageActionFilter>();
services.Configure<RequestLocalizationOptions>(
options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("pt-BR")
};
options.DefaultRequestCulture = new RequestCulture(culture: "pt-BR", uiCulture: "pt-BR");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
EDIT: I had not made clear my problem ... Although I put the above information in the statup.cs the dates on my Controller gets mm / dd / yyyy, ie I have a calendar in the application, the user chooses the date in the Picker and send record with date 08/20/2017 and error, but if it fill the date 08/20/2017 it works.
In my VIEW:
@*MODAL REGISTRAR AGENDA*@
<div class="modal fade" id="ModalAgenda" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg lg-effect-10" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Registro de Compromisso</h3>
</div>
<div class="modal-body">
<div class="row no-padding">
<div class="col-sm-12" style="padding-left: 0; padding-right: 0;">
<div class="panel">
<div class="panel-body">
<div class="row">
<div class="form-group col-sm-4">
<label>Tipo</label>
<select class="form-control" name="tipoAgenda" id="tipoAgenda">
<option selected="selected">SELECIONE</option>
<option value="1">CONTATO</option>
<option value="2">VISITA</option>
<option value="3">REUNIAO</option>
<option value="4">TOUR COMERCIAL</option>
<option value="5">APRESENTAÇÃO</option>
<option value="6">ASSEMBLÉIA</option>
</select>
</div>
<div class="form-group col-sm-8">
<label class="control-label">Título</label>
<input class="form-control" name="Titulo" id="Titulo" data-val="true" />
</div>
</div>
<div class="row">
<div class="form-group col-sm-12">
<label class="control-label">Descrição</label>
<textarea class="form-control" name="Descricao" id="Descricao" data-val="true" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-sm-4" id="data1">
<label class="control-label">Data</label>
<div class="input-group date">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span><input type="text" name="Data1" id="Data1" class="form-control">
</div>
</div>
<div class="form-group col-sm-2">
<label class="control-label">Hora Início</label>
<div class="input-group clockpicker" data-autoclose="true">
<input type="text" name="HoraInicio" id="HoraInicio" class="form-control">
<span class="input-group-addon">
<span class="fa fa-clock-o"></span>
</span>
</div>
</div>
<div class="form-group col-sm-2">
<label class="control-label">Hora Fim</label>
<div class="input-group clockpicker" data-autoclose="true">
<input type="text" name="HoraFim" id="HoraFim" class="form-control">
<span class="input-group-addon">
<span class="fa fa-clock-o"></span>
</span>
</div>
</div>
</div>
<br />
<div class="row">
<div class="form-group col-sm-2">
<label class="control-label">Notificar</label>
<input type="text" name="Notifica" id="Notifica" class="form-control" style="text-align:center" value="30" />
</div>
<div class="form-group col-sm-4">
<label class="control-label">Antes do agendamento</label>
<select class="form-control" name="tipoAlerta" id="tipoAlerta">
<option selected="selected" value="1">MINUTOS</option>
<option value="2">HORAS</option>
<option value="3">DIAS</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-sm btn-danger" data-dismiss="modal">Cancelar</button>
<button type="button" class="btn btn-success" onclick="GravaAgenda()">Gravar</button>
</div>
</div>
</div>
</div>
Mu Js that sends the information to the Controller
function GravaAgenda() {
var prospect = $("#IdProspect").val();
var tipoagenda = $('#tipoAgenda option:selected').val();
var titulo = $("#Titulo").val();
var descricao = $("#Descricao").val();
var data1 = $("#Data1").val();
var horainicio = $("#HoraInicio").val();
var horafim = $("#HoraFim").val();
var notifica = $("#Notifica").val();
var tipoalerta = $('#tipoAlerta option:selected').val();
//Gravar
var url = "/Agenda/GravaAgenda";
$.ajax({
url: url,
datatype: "json",
data: { 'prospect': prospect, 'tipoagenda': tipoagenda, 'titulo': titulo, 'descricao': descricao, 'data1': data1, 'horainicio': horainicio, 'horafim': horafim, 'notifica': notifica, 'tipoalerta': tipoalerta },
type: "POST",
success: function (data) {
swal({ type: "success", title: "Gravado com sucesso!", timer: 2000, showConfirmButton: false });
window.location.reload();
}
});
};
My Controller:
public async Task GravaAgenda(string prospect, string tipoagenda, string titulo, string descricao, string data1, string horainicio, string horafim, string notifica, string tipoalerta)
{
var ageData = new AgendaData();
var user = await _userManager.GetUserAsync(User);
string usuario = user.Id;
int empresa = user.IdEmpresa;
int Idprospect = Convert.ToInt32(prospect);
int minutos = 0;
var tipoAgenda = TipoAgenda.Contato;
switch (tipoalerta)
{
case "3":
minutos = Convert.ToInt32(notifica) * 24 * 60;
break;
case "2":
minutos = Convert.ToInt32(notifica) * 24;
break;
case "1":
minutos = Convert.ToInt32(notifica);
break;
}
switch (tipoagenda)
{
case "1":
tipoAgenda = TipoAgenda.Contato;
break;
case "2":
tipoAgenda = TipoAgenda.Visita;
break;
case "3":
tipoAgenda = TipoAgenda.Reuniao;
break;
case "4":
tipoAgenda = TipoAgenda.Tour;
break;
case "5":
tipoAgenda = TipoAgenda.Apresentacao;
break;
case "6":
tipoAgenda = TipoAgenda.Assembleia;
break;
}
var dataInicio = Convert.ToDateTime(data1 + " " + horainicio);
var dataFim = Convert.ToDateTime(data1 + " " + horafim);
var dataAlerta = dataInicio.AddMinutes(-minutos);
await ageData.GravaAgenda(Idprospect, usuario, titulo, descricao, dataInicio, dataFim, dataAlerta, tipoAgenda, empresa);
await new ProspectLogData().RegitroContato(Idprospect, usuario, "REGISTRO DE AGENDAMENTO DO PROSPECT.", "8");
RedirectToAction("Index");
}
My recording layer in the Bank
public async Task GravaAgenda(int Idprospect, string usuario, string titulo, string descricao, DateTime dataInicio, DateTime dataFim, DateTime dataAlerta, TipoAgenda tipoAgenda, int empresa)
{
using (var db = new CRMContext())
{
var agenda = new Agenda();
agenda.IdProspect = Idprospect;
agenda.IdUsuario = usuario;
agenda.Titulo = titulo.ToUpper();
agenda.Descricao = descricao.ToUpper();
agenda.DataInicio = dataInicio;
agenda.DataFim = dataFim;
agenda.DataAlerta = dataAlerta;
agenda.Criado = DateTime.Now;
agenda.TipoAgenda = tipoAgenda;
agenda.IdEmpresa = empresa;
db.Agenda.Add(agenda);
await db.SaveChangesAsync();
}
}