Unique name of uploaded files

If you want to prevent situation that you have the same name of upload files use this example: var fileToSave =  DateTime.Now.ToString(“yyyyMMddHHmmssfff”) + “_” + Guid.NewGuid().ToString(“N”) + “-” + SomeYourModelsId + “.pdf”;  

Hide DIV with DropDownList

Sometimes we need to hide some static part of the view. With help comes by jQuery and CSS. First we need add script like below: $(‘.divrek’).hide(); $(‘.extensionFields’).change(function () { if ($(this).val() == ‘1’) { $(‘.divrek’).show(); } else { $(‘.divrek’).hide(); }; }; }); Use ordinary DropDownList with ignition CSS Class: extensionFields @Html.DropDownList(“Id”, null, htmlAttributes: new { […]

Prevent duplicate records with seed

Prevent duplicate records when you use seed method in migration. Originally in Migrations/Configuration.cs we can use: context.People.AddOrUpdate( p => p.FullName, new Person { FullName = “Andrew Peters” }, new Person { FullName = “Brice Lambson” }, new Person { FullName = “Rowan Miller” } ); But when we configure adding record (like sample above) every […]

Observations: ALTER TABLE statement conflict

If you add another table to your project with 1-to-many relationships you may meet an error sound like: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint “FK_dbo.tableModels_dbo.idModels_admIdModelsId”. The conflict occurred in database “aspnet-dwmp1-20170323101827”, table “dbo.tableModels”, column ‘IdModelsId’. Solved of this problem lies in content of table where you hook up your new table. In other […]

Some commands: Rollback migration

1.In Package Manager Console command Get-migrations return listing with migration names that you made: Get-Migrations 2. Update db for earlier state: Update-Database –TargetMigration: <name-from-listing-above> 3. Delete file with incorrect migration from your migrations folder in Solution Explorer. 4. Create new correct migration and update db.

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 […]

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 […]

Some commands: Delete database in project

When we need to delete our database files and create new one. 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 […]