Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

c# - Why I get another Date format in my Index Page

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 ?

question from:https://stackoverflow.com/questions/65832941/why-i-get-another-date-format-in-my-index-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...