loading date time without javascript

0

I wanted to know if it is possible to send direct from the controller, for example the current date and time of the system without using javascript. Ex:

<div class="form-group">
    @Html.LabelFor(model => model.DataAvaliacao, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.DataAvaliacao, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.DataAvaliacao, "", new { @class = "text-danger" })
    </div>
</div>
// GET: Avaliacao
public ActionResult Create()
{
    DateTime data = DateTime.Now;//Essa data ao carregar a tela aparece para o usuário
    return View();
}

Type as if it were the Viewbag and then the time the screen loads, the date and time appear

    
asked by anonymous 01.02.2017 / 00:54

2 answers

0

Well you can pass this object through a ViewBag or through your model , below is an example of how to do these two forms, the first example being the demo of Viewbag usage.

public ActionResult Create()
{
    ViewBag.Data = DateTime.Now;
    return View();
}

done this just access the viewbag in your View

In your html / razor you can simply do so if you want to display the date value

<strong> @ViewBag.Data </strong>

If you are interested in setting this date as the default value of your DataAvalation field. One option is to initialize the object relative to the model in the controller, fill it in, and then send it to the view.

public ActionResult Create()
{ 
    var model = new SeuTipoDeObjeto();
    model.DataAvaliacao = DateTime.Now;
    return View(model);
}

In this way the value of your textbox will be filled with the current date.

    
01.02.2017 / 04:13
0

Have you tried it on Razor?

@Html.EditorFor(model => model.DataAvaliacao, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Now/*.ToShortDateString()*/ } })
    
17.02.2017 / 18:18