Current date and time - Default in MVC razor

后端 未结 4 676
梦如初夏
梦如初夏 2020-12-14 20:30

When the MVC view page with this textbox, loads , I would like to display current date and time by default. How can I do this? in razor.

  @Html.EditorFor(mo         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 21:04

    You could initialize ReturnDate on the model before sending it to the view.

    In the controller:

    [HttpGet]
    public ActionResult SomeAction()
    {
        var viewModel = new MyActionViewModel
        {
            ReturnDate = System.DateTime.Now
        };
    
        return View(viewModel);
    }
    

提交回复
热议问题