Home > Software design >  Razor Page ignores DisplayFormat
Razor Page ignores DisplayFormat

Time:07-06

I'm saving a date in a model like this:

[Display(Name = "Eintrittsdatum")]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = false)]
[Required]
public DateOnly EntryDate { get; set; }

But when I try to display it in my view it does this. It doesn't matter if the DisplayFormat line is there or not, or what it says there, I can write just about anything, the program completely ignores that line.

After a bit of searching I found this solution.

Now my dates are correctly displayed but in an editable text box, which isn't really what I want. Does anyone have a suggestion for a better solution or an explanation why my date looks like it does in the first picture?

Index.cshtml:

        @foreach (var item in Model.Employee)
            {
            ...
            <td>
                @Html.TextBoxFor(modelItem => item.EntryDate, "{0:dd.MM.yyyy}", new {maxLength = 10})
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            ...

Index.cs:

public class IndexModel : PageModel
{
    private readonly PersonaleinstellungContext _context;

    public IndexModel(PersonaleinstellungContext context)
    {
        _context = context;
    }

    public IList<Employee> Employee { get; set; }
    [BindProperty(SupportsGet = true)]
    public string SearchString { get; set; }
    public SelectList Departments { get; set; }
    [BindProperty(SupportsGet = true)]
    public string EmpDepartment { get; set; }

    public async Task OnGetAsync()
    {
        IQueryable<string> depQuery = from e in _context.Employee
                                      orderby e.Department
                                      select e.Department;

        var employees = from e in _context.Employee
                        select e;
        if (!string.IsNullOrEmpty(SearchString))
        {
            employees = employees.Where(s => s.LName.Contains(SearchString));
        }

        if (!string.IsNullOrEmpty(EmpDepartment))
        {
            employees = employees.Where(x => x.Department == EmpDepartment);
        }
        Departments = new SelectList(await depQuery.Distinct().ToListAsync());

        Employee = await employees.ToListAsync();
    }
}

CodePudding user response:

To format the display of a DateTime, you can simply provide a format string to the ToString method (as well as the salary):

@foreach (var item in Model.Employee)
{
    <td>
        @item.EntryDate.ToString("dd.MM.yyyy")
    </td>
    <td>
        @item.Salary.ToString("c")
    </td>
    ...

Another thing - don't use Html helpers for rendering form fields in ASP.NET Core. Use tag helpers instead. Set the DataType to Date, the format string to an ISO 8601 string that the input type="date" understands, ApplyFormatInEditMode to true and let Razor Pages take care of the rest:

[Display(Name = "Eintrittsdatum")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DateType.Date)]
[Required]
public DateOnly EntryDate { get; set; }


<input asp-for="EntryDate" />

https://www.learnrazorpages.com/razor-pages/forms/dates-and-times

  • Related