Bug dd.MM.yyyy format

For right validation of format date like: dd.MM.yyyy in MVC5 you need to do cuple of things.

1. In NuGet Menager Console install:

Install-Package Moment.js

2. In _Layout.cshtml add:

//At HEAD part add:
@Scripts.Render(“~/Scripts/jquery.validate.min.js”)
@Scripts.Render(“~/Scripts/jquery.validate.unobtrusive.min.js”)
@Scripts.Render(“~/Scripts/moment.js”)

3. In menu Tools / Extensions and Updates install From jQuery-ui, add link to jquery-ui and script datepicker with individual language corrections:

<link href=”@Url.Content(“~/Content/themes/base/jquery-ui.min.css”)” rel=”stylesheet” />

<script type=’text/javascript’>
jQuery(function($) {
$(‘.date-picker’).datepicker({
duration: ”,
changeMonth: false,
changeYear: false,
yearRange: ‘2000:2030’,
showTime: false,
time24h: true
});

$.datepicker.regional[‘pl’] = {
closeText: ‘Zamknij’,
prevText: ‘Wstecz’,
nextText: ‘W przód’,
currentText: ‘Nyní’,
monthNames: [‘Styczeń’, ‘Luty’, ‘Marzec’, ‘Kwiecień’, ‘Maj’, ‘Czerwiec’, ‘Lipiec’, ‘Sierpień’,
‘Wrzesień’, ‘Październik’, ‘Listopad’, ‘Grudzień’
],
monthNamesShort: [‘Sty’, ‘Lty’, ‘Mar’, ‘Kwi’, ‘Maj’, ‘Cze’, ‘Lip’, ‘Sie’, ‘Wrz’, ‘Paź’, ‘Lis’, ‘Gru’],
dayNames: [‘Niedziela’,’Poniedziałek’, ‘Wtorek’, ‘Środa’, ‘Czwartek’, ‘Piątek’,’Sobota’],
dayNamesShort: [‘Nd’, ‘Po’, ‘Wt’, ‘Śr’, ‘Cz’, ‘Pt’, ‘So’],
dayNamesMin: [‘Nd’, ‘Po’, ‘Wt’, ‘Śr’, ‘Cz’, ‘Pt’, ‘So’],
weekHeader: ‘Tydz’,
dateFormat: ‘dd.mm.yy’,
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ”
};

$.datepicker.setDefaults($.datepicker.regional[‘pl’]);
});

4.  //validation of date type dd.mm.yyyy

$(function () {
$.validator.methods.date = function (value, element) {
return this.optional(element) || moment(value, “DD.MM.YYYY”, true).isValid();
}

5.  //disable datepicker from Google Chrome

$(‘input[type=”date”]’).attr(‘type’, ‘text’);

6. //start datepicker when HtmlHelper call class=”date-picker”

$(‘.date-picker’).datepicker();
})

</script>

7. In those files different types of helpers:
For Create.cs:

@Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = “date-picker form-control” } })

For Edit.cs:

@Html.TextBoxFor(model => model.Data, “{0:dd.MM.yyyy}”, new { @class = “date-picker form-control” })

Some commands: Enable Database Migrations

If you want to apply database migrations in your project just type in NuGat Console those command:

enable-migrations

After every change of structure in your database model need to called at migration command with your custom migration name:

add-migration <name-of-change>

Now it’s time to database update command:

update-database

and for the end – if something went wrong you may won’t go back to initial state of your database, use this command:

update-database -TargetMigration $InitialDatabase

Some commands: Delete database in project

Sometimes we need to delete our database files and create new one. When we delete file *.dbf from app_data folder in our project and tray to bulding new up we get some errors. In this case go to the windows command line (cmd.exe) and stop your SQL Local DB Serwer and delete SQL Local DB Serwer with those two commands:

sqllocaldb.exe stop
sqllocaldb.exe delete

After that, your database its crate as new