.NET
Microsoft Technologies based on the .NET software framework.
1,139 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello ,
I have a Form that contains two input fields of type Time (Start time , End Time) after submitting the form I want to Show The Information in the View Like this (10:30 AM) I try Display Format but it's not working
or is it better to use String to save the time instead of DateTime (hint : I'm using the Start , End to query something between these time)
Here is My Model
public class Shifts
{
public int Id { get; set;}
[DisplayFormat(DataFormatString = "{dddd, dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{hh:mm tt}")]
public DateTime EndTime { get; set; }
}
In the view
@foreach (var item in Model.ShiftsList)
{
<tr>
<td></td>
<td hidden>@item.Id</td>
<td>@item.Name</td>
<td>
@Html.DisplayFor(x => item.StartTime)
</td>
<td>
@Html.DisplayFor(x => item.EndTime)
</td>
<td>
<button class="btn btn-info" id="viewbtn">Edit</button>
</td>
</tr>
}
I'm not sure what's wrong on your end. The code you've shared works perfectly.
public class Shifts
{
public int Id { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dddd, dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:hh:mm tt}")]
public DateTime EndTime { get; set; }
}
public class ShiftVm
{
public List<Shifts> ShiftsList { get; set; }
}
public ActionResult Index()
{
ShiftVm vm = PopulateVm();
return View(vm);
}
private ShiftVm PopulateVm()
{
return new ShiftVm()
{
ShiftsList = new List<Shifts>
{
new Shifts() { Id = 1, StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(8) },
new Shifts() { Id = 2, StartTime = DateTime.Now.AddHours(24), EndTime = DateTime.Now.AddHours(32) },
}
};
}
@model MvcIdentityDemo.Models.ShiftVm
@{
ViewBag.Title = "Home Page";
}
<table class="table">
@foreach (var item in Model.ShiftsList)
{
<tr>
<td></td>
<td>
@Html.DisplayFor(x => item.StartTime)
</td>
<td>
@Html.DisplayFor(x => item.EndTime)
</td>
<td>
<button class="btn btn-info" id="viewbtn">Edit</button>
</td>
</tr>
}
</table>