Sunday, 24 January 2016

DMY date formatting in asp.net MVC and Entity framework

Most DevOps types in the world have at some time been frustrated by date formatting and database collation. Its no wonder, given that the standard in most of the world is DMY with the exception being the country from which most software platforms and languages originate which uses MDY (see https://en.wikipedia.org/wiki/Date_format_by_country )  Its a problem that can cause real pain and is not always obvious as for the first 11 days of each month, you will not get any errors. I've had to deal with this one several times when moving apps between environments where the database collation is not updated correctly.

asp.net and Entity framework fortunately do make it a bit easier when developing software for the DMY and YMD parts of the world. There are two things that you need to do.

1) In the site web.config, add to <system.web>  an element like:
<globalization culture="en-AU" uiCulture="en-AU" />
(change the culture and  uiCulture to match your culture, this example is for Australia)


2) Add a date format attribute in the model class to present the correct format, for example:

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

Now all the server and client side validations will correctly validate dates using this format and the data in the database will also be in this format.

This works with asp.net version 5, in the community edition of Visual studio (downloaded at the end of 2015) For older projects, you may need to update some jQuery libraries. This post from stackoverflow may be helpful in that case - http://stackoverflow.com/questions/18548816/mvc-4-date-culture-issue 

Monday, 11 January 2016

Create a menu that expands onmouseover using ASP.NET MVC and Bootstrap

To add a dynamic menu that expands when hovering a mouse over the top in a ASP.NET MVC project that uses Bootstrap, make the following changes. This example will highlight the item selected and also expands/conntracts a sub menu on a mouse-over event.

1. In _Layout.cshtml (under \Views\Shared), add the top level menu items that you need by editing the default items provided by visual studio in <div class="navbar-collapse collapse">. The code will look something like this:

<div class="navbar-collapse collapse">
     <ul class="nav navbar-nav">
          <li>@Html.ActionLink("Item1", "Index", "Item1")</li>
          <li>@Html.ActionLink("Item2", "Index", "Item2")</li>

          <li>@Html.ActionLink("About", "About", "Home")</li>
          <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
     </ul>
     @Html.Partial("_LoginPartial")
</div>

2. To add the next level of menu (in this case, a menu called Item3, which has 3 sub menus), add the following code. The <span class="caret"> will put a small triangle to indicate that this is a drop down menu. You could replace this with an  image if desired.


<li class=dropdown>
     <a class="dropdown-toggle" data-toggle="dropdown" href="#">Item3 &nbsp; <span class="caret"></span></a>
     <ul class="dropdown-menu" role="menu">
         <li>@Html.ActionLink("SubMenuA", "Index", "MenuA")</li>
         <li>@Html.ActionLink("SubMenuB", "Index", "MenuB")</li>
         <li>@Html.ActionLink("SubMenuC", "Index", "MenuC")</li>
     </ul>
</li>

3.Before the </body> tag, add this bit of JavaScript. It would be a good idea to bundle and minify this this code, particularly if it is being used for a high traffic site or it is hosted by a cloud service. See http://www.asp.net/mvc/overview/performance/bundling-and-minification for more details.

<script>
$(function () {
$('.dropdown').hover(function () {
$(this).addClass('open');
},
function () {
$(this).removeClass('open');
});
});
</script>

Thursday, 7 January 2016

Welcome to my tech blog. On this blog I plan to post some brief tutorials on subjects that I hope may help others learn something new. There are many ways to pat a cat, just be sure to choose one that does not end with claws and teeth :-) So it is with developing software. One of my aims is to share with others techniques and code fragments that have worked for me and will hopefully work for others.