以(asp.net核心)格式显示今天的日期

问题描述 投票:2回答:2

---------我想在我的电影创作表单中显示今天的日期。----------------

控制器:

    // GET: Movies/Create
    public IActionResult Create()
    {

        ViewData["IdGenre"] = new SelectList(_context.Genres, "IdGenre", "Name");
        return View();
    }

    // POST: Movies/Create
    // To protect from overposting attacks, enable the specific properties you want to bind to, for 
    // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,Name,DateDeSortie,Stock,IdGenre")] Movie movie)
    {


        if (ModelState.IsValid)
        {

            _context.Add(movie);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        ViewData["IdGenre"] = new SelectList(_context.Genres, "IdGenre", "Name", movie.IdGenre);
        return View(movie);
    }

型号:

 [DataType(DataType.Date)]
    [Display(Name = "Date de sortie")]
    [Required(AllowEmptyStrings =false,ErrorMessage ="La date est requise")]
    public DateTime? DateDeSortie { get; set; }

查看:

 <div class="form-group">
            <label asp-for="DateDeSortie" class="control-label"></label>
            <input asp-for="DateDeSortie" class="form-control" />
            <span asp-validation-for="DateDeSortie" class="text-danger"></span>
        </div>

感谢您的帮助,我被困了几天

c# asp.net datetime asp.net-core asp.net-mvc-4
2个回答
2
投票

将此代码添加到您的View。这将是今天的日期。这可能是您想要的,也可能不是。

@Html.Label("DateCreated", DateTime.Today.ToShortDateString().ToString(), new { @readonly = "readonly" })

enter image description here


1
投票

在我的观点中,您可以直接将创建视图的输入标签的值设置为今天的值。为了解决这个问题,我们可以使用js或直接使用@DateTime.Now.ToString("yyyy-MM-dd")

完成

更多详细信息,您可以参考以下代码:

        <div class="form-group">
            <label asp-for="DateDeSortie" class="control-label"></label>
            <input asp-for="DateDeSortie" class="form-control" value="@DateTime.Now.ToString("yyyy-MM-dd")"  />
            <span asp-validation-for="DateDeSortie" class="text-danger"></span>
        </div>

结果:

enter image description here


0
投票

非常感谢您的帮助,它有效

© www.soinside.com 2019 - 2024. All rights reserved.