Send date and time to the mvc asp .net driver

1

Good day, I have a small problem with the dates when sending them to the controller (POST) of the view. This is my form:

<form class="form-horizontal" method="post" action="/home/insert" novalidate>
<div class="col-sm-3 input-group" style="float: left;">
   <input type="text" class="date start form-control" placeholder="yyyy/mm/dd" required style="border-right: 0;"value="@string.Format("{0:MM-dd-yyyy}", Model.evento[0].Fecha_Inicio)" />
   <span class="input-group-addon fui-calendar" style="background: transparent; color: #bdc3c7;"></span>
</div>
<div class="col-sm-2 input-group" style="float: left; width: 20.9%;">
    <input type="text" class="time start form-control" placeholder="Hora Inicio" required style="border-right: 0;" value="@string.Format("{0:HH:mm:ss}", Model.evento[0].Fecha_Inicio)" />
    <span class="input-group-addon fui-time" style="background: transparent; color: #bdc3c7;"></span>
</div>
</form>

This is my model:

public class insertEvento
{
    public string Fecha_Inicio { get; set; }
    public string Hora_Inicio { get; set; }
}

That way it works fine but what I really want is to send the date to my model in this way

public class insertEvento
{
    public DateTime Fecha_Inicio { get; set; }
}

But without having to modify my fields of the form since I am using third-party libraries that in the date field only gives me the date and in the time field only the time in 24 hours format

    
asked by JuankGlezz 13.05.2016 в 22:22
source

1 answer

1

This happens because of the culture applied to the site, you could try defining the line% of the line

<globalization uiCulture="es" culture="es-ES" />

with this, if you enter a date with the format web.config you should recognize it and be able to assign it in the action.

If this does not work, you will have to define a custom ModelBinder that allows you to parse the date that you receive as a string

Globalizing DateTime and Decimal in ASP.NET MVC Using ModelBinder

Globalization: Model binding DateTimes with ASP.Net MVC

this form is something more particular since you would be taking the value and validating the date from code to adapt it to the data type

    
answered by 13.05.2016 / 23:02
source