load date time without javascript

Asked

Viewed 187 times

0

I wanted to know if it is possible to send directly 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();
}

Like it was the Viewbag and then at the time the screen load, appear the date and time

  • With ViewBag !!!

2 answers

0

Well you can pass this object by a Viewbag or through your model, below follows the example of how to make these two forms, the first example being the demonstration of use of Viewbag.

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 placing this date as the value pattern of your Datarating field. One option is to initialize the model object in the controller, fill it, and then send it to the view.

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

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

0

You already tried Razor ?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.