ToStringMethodAndIFormatProvider

CSharp · Mvc

Model

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
	public class SampleViewModel
	{
		public DateTime? AuditDate
		{
			get;
			set;
		}
	}
}

View

@model HelloWorldMvcApp.SampleViewModel
@{
	Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Bootstrap 101 Template</title>

		<!-- CSS Includes -->
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
		
		<style type="text/css">

			.field-validation-error {
				color: #ff0000;
			}

		</style>
	</head>

@helper DateFormatter(object date)
{
    var shortDateLocalFormat = "";
    if (date != null) {     
       shortDateLocalFormat = ((DateTime)date).ToString("d", new System.Globalization.CultureInfo("en-US"));
     }

    @shortDateLocalFormat
}

	
	<body>
		<div class="container">
			<div class="col-md-6 col-md-offset-3">
				<h3>AuditDate</h3>
	
				@using (Html.BeginForm())
				{
					<div class="form-group">
						@DateFormatter(Model.AuditDate)
					</div>
				
				}
	
				<br/><br/>
				<div class="alert alert-warning fade">
					<img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/>
					<strong><span class="alert-content"></span></strong>
				</div>
			</div>
		</div>

		<!-- JS includes -->
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
		<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
	
	</body>
</html>

Controller

using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Threading;

namespace HelloWorldMvcApp
{
	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			SampleViewModel svm = new SampleViewModel();
			svm.AuditDate = new DateTime(2017, 1, 31);
			return View(svm);
		}
	}
}
;