question

GcobaniMkontwana-0775 avatar image
0 Votes"
GcobaniMkontwana-0775 asked AgaveJoe edited

How to display date and time in a row table in asp.net mvc?

Hi team\

I want to display date and time from this logic below, some how it doesnt appear so good and need some help.

// Controller
public class TimeLineDashboardController : Controller
{
// GET: TimeLineDashboard
public ActionResult Index()
{

         // var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
          var nwDt = DateTime.Now.ToShortDateString();
          ViewData["nowDt"] = nwDt;
          return View();
      }
  }

// Model
public class Dashboard
{

      // Map all values from the record list in TimeLine Application DB.
      [Key]
      [Display(Name = "Incident")]
      public int Incident { get; set; }
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd HH:mm}")]
      public DateTime dateTime { get; set; }
      [Display(Name = "Resource")]
      public string Resources { get; set; }
      [Display(Name = "Description")]
      public string Description { get; set; }
      [Display(Name = "EndDate")]
      public int EndDate { get; set; }

}

// View
<h2>TimeLine</h2>

<div style="width:100%">
<table style="width:100%">
<tr>
<td style="width: 50%;">
@ViewData["nowDt"]
</td>

          <td style="width: 50%;">
              @ViewData["nowTm"]
          </td>
      </tr>
  </table>

</div>




dotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

The standard MVC pattern passes a view model to a View. You get to format the date however you like within the view's HTML (Razor).

Standard date and time format strings
Custom date and time format strings
Get started with ASP.NET Core MVC

 namespace MvcBasic.Controllers
 {
    
     public class ViewModel
     {
         public DateTime dateTime { get; set; }
     }
    
     public class HomeController : Controller
     {
         private readonly ILogger<HomeController> _logger;
         public HomeController(ILogger<HomeController> logger)
         {
             _logger = logger;
         }
    
         public IActionResult Index()
         {
             ViewModel vm = new ViewModel() { dateTime = DateTime.Now };
             return View(vm);
         }
      }
 }


View

 @model MvcBasic.Controllers.ViewModel
    
 @{
     ViewData["Title"] = "Index";
 }
    
 <h1>Index</h1>
    
 <h4>ViewModel</h4>
 <hr />
 <table>
     <tr>
         <td>
             DateTime: @Model.dateTime.ToString("MM/dd/yyyy hh:mm tt")
         </td>
     </tr>
 </table>


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.