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();
}
}