0

I'm working with asp.net core in visual studio 2017

I will need to use C#, MVC, or SQL to solve this

I'm creating a table with a foreach statement (below)

        <table>
           <tbody>
                @foreach (var item in Model)
                {
                <tr>

                    <td>
                        @Html.DisplayFor(modelItem => item.Month)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.Temperature)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.StartDate)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.EndDate)
                    </td>

                </tr>
                }
            </tbody>
        </table>

and I'm trying to modify this model data to display only the Date.

        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

How would I do this so that all of my model items are effected?

Here are my controller actions

 public IActionResult Create()
        {
            return View();
        }

and

        [HttpPost]
        public async Task<IActionResult> Create(Data data)
        {
            if (ModelState.IsValid)
            {
                _context.Add(data);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(data);
        }
Peter B
  • 22,460
  • 5
  • 32
  • 69
FIVE MANGO
  • 21
  • 1
  • 6
  • 1
    Possible duplicate of [Converting DateTime format using razor](https://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor) – Jonathon Chase Aug 13 '19 at 14:49
  • Could you please explain what do you mean by _How would I do this so that all of my model items are effected?_ – prinkpan Aug 13 '19 at 15:05

2 Answers2

1

You can annotate your model with DisplayFormat attributes:

using System.ComponentModel.DataAnnotations;

public class DateModel
{
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime StartDate { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime EndDate { get; set; }
}

Or if you want to avoid repeating these, you can also use a constant:

using System.ComponentModel.DataAnnotations;

public class DateModel
{
    [DisplayFormat(DataFormatString = Constants.DateOnlyFormat)]
    public DateTime StartDate { get; set; }

    [DisplayFormat(DataFormatString = Constants.DateOnlyFormat)]
    public DateTime EndDate { get; set; }
}

public static class Constants
{
    public const string DateOnlyFormat = "{0:MM/dd/yyyy}";
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • How would I display only the Date inside of my form? – FIVE MANGO Aug 13 '19 at 15:30
  • Which form? There is no such thing in your question, you are only displaying values. But in case you want to also apply it for **input** fields, here is how that could be done: `[DisplayFormat(DataFormatString = DateConstants.DateOnlyFormat, ApplyFormatInEditMode = true)]`. – Peter B Aug 14 '19 at 07:50
0

Use the Date property of DateTime structure.

<td>
      @Html.DisplayFor(modelItem => item.StartDate.Date.ToString("d"))
</td>

For more details refer :

Praveen Rai
  • 777
  • 1
  • 7
  • 28