0

I notice one mistake in my Index Page and I try to figure out what happen. When I try to insert Date in create View I get right format, but Index Page show different date format.

Here is my IndexPage.cshtml

@model IEnumerable<BergClinics.Models.AdmissionPacients>

@{
    ViewData["Title"] = "Index";
}

<div class="container p-3 bg-white">
    <div class="row pt-4">
        <div class="col-6">
            <h2 class="text-primary">Admission Patient List</h2>
        </div>
        <div class="col-6 text-right">
            <a asp-action="Upsert" class="btn btn-primary">
                <i class="fas fa-plus"></i> &nbsp; Create New Doctor
            </a>
        </div>
    </div>

    <br /><br />

    @if (Model.Count() > 0)
    {
        <table class="table table-bordered table-striped" style="width:100%">
            <thead>
                <tr>
                    <th>
                        Doctor Full Name - CODE
                    </th>

                    <th>
                        Patient Full Name
                    </th>
                    <th>
                        Date and Time
                    </th>
                    <th>
                        Emergency
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var obj in Model)
                {
                    <tr>
                        <td width="25%">@obj.Doctor.Firstname @obj.Doctor.Lastname @obj.Doctor.Code</td>
                        <td width="25%">@obj.Patient.FirstName @obj.Patient.LastName</td>
                        <td width="25%">@obj.DateAndTime</td>
                        <td width="25%">@obj.Emergency</td>



                        <td class="text-center">
                            <div class="w-75 btn-group" role="group">
                                <a asp-route-Id="@obj.Id" asp-action="Upsert" class="btn btn-primary mx-2">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a asp-route-Id="@obj.Id" asp-action="Delete" class="btn btn-danger mx-2">
                                    <i class="far fa-trash-alt"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else
    {
        <p> No Admission Patient exists.</p>
    }

</div>

In Model I add

[Required]
[Display(Name = "Date and Time")]
public DateTime DateAndTime { get; set; }

And here is what I get as output:

enter image description here

enter image description here

What should I change in order to represent DateTime correctly here. So If I put date like 1/21/2020 18:31 I also want this date and time be represent in my IndexPage as well. ? Any ideas ?

MirDer
  • 61
  • 6
  • You can control the formatting of a `DateTime` object when you display it, using either [Standard DateTime Formatting](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) or [Custom DateTime Formatting](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings). Is that what you're asking? – Rufus L Jan 21 '21 at 18:04
  • Yes, exactly that – MirDer Jan 21 '21 at 18:05
  • Since when I create Patient I send format like 1/21/2020 and in Index Page I get somethinl like 0001/01/01 12:00:00 – MirDer Jan 21 '21 at 18:07
  • See the answers to [this question](https://stackoverflow.com/questions/6001654/how-to-render-a-datetime-in-a-specific-format-in-asp-net-mvc-3) and see if they help. The result you're showing in the comment above is the default format (`.ToString()`) for the default value (`0`) of a `DateTime` instance, so there may be some other issue in the way you're receiving the date. – Rufus L Jan 21 '21 at 18:09
  • How to solve this problem ? I need to send data from Create View to Index View ? So If user inser 1/21/2021 19:15 it needs to display exactly as is inserted ? – MirDer Jan 21 '21 at 18:13
  • If you want to output the same format that the user used to enter the date rather than a consistent format across your app, then the easiest would probably be to capture their input string and use a string field to store the data (but use `DateTime` to parse it). Note that this is a bad way to get a date from a user, since different regions have different, conflicting formats. Some do `dd/mm/yyyy` and others do `mm/dd/yyyy` for example. So you don't really know if `01/02/2021` is January 2nd or February 1st. – Rufus L Jan 21 '21 at 18:22

1 Answers1

0

you should be able to apply a format on the model. Like this:

[Required]
[Display(Name = "Date and Time")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DateAndTime { get; set; }

just edit to be the format you want.

Josh
  • 188
  • 1
  • 12