Place current date in a TextBox in C #

0

I need the current date to be loaded in the TextBox ; I'm doing it with the following code but when I run it, it does not load any date:

<td>
    <label for="Fecha">Fecha:</label>
</td>
<td>                            
    @Html.TextBoxFor(model => model.Facturas.Fecha, new { @class = "form-control  input-sm", @Type= "date", value = System.DateTime.Today.ToShortDateString()})
</td>

I use C # MVC.

    
asked by Jhon Castrillon 11.06.2017 в 21:24
source

2 answers

1

In your model you can put a default value, in this case, the current date using get { return DateTime.Now; } , assuming the property is called FechaActual :

public DateTime FechaActual { 
    get { return DateTime.Now; } 
    set { this.FechaActual = value; } 
}

In the view, you should not make any changes, simply put the ownership of the model as follows:

@Html.TextBoxFor(model => model.FechaActual, new {@class="form-control"})

Here you can see the demonstration of the solution .

    
answered by 11.06.2017 в 23:55
0

You have to use the Today property:

DateTime today = DateTime.Today;

Review the documentation: link

    
answered by 11.06.2017 в 22:46