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

Gcobani Mkontwana 21 Reputation points
2021-09-13T12:17:10.873+00:00

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>

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2021-09-13T13:06:25.937+00:00

    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>  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful