ASP.NET MVC Multiple Check-boxes

http://www.shawnmclean.com/blog/2011/04/asp-net-mvc-multiple-check-boxes-in-an-array-or-list/

Advertisements

ASP.NET MVC Date Validation

I had the following property in my view model:

        [Required(ErrorMessage = "Du måste fylla i från-datum och -tid")]
        public DateTime? From { get; set; }

When I entered “2014-01-01 00:00:00” I got the following validation error: “The field From must be a date.” So, it the built in validation somehow assumes a date without time.

My next attempt was to add a another attribute:

        [DataType(DataType.DateTime)]

This had no effect. My third attempt was to use a regular expression. I defined a new attribute that inherits RegularExpressionAttribute:

    public class DateTimeValidationAttribute : RegularExpressionAttribute
    {
        public DateTimeValidationAttribute()
            : base(@"^((20\d{2})-((0[1-9])|(1[0-2]))-(0[1-9]|1\d|2[0-8])|((20\d{2})-(((0[1,3-9]|1[0-2])-((29|30)))|((0[13578]|1[02])-31)))|(((20(([02468][048])|([13579][26]))))-02-29)) (([0-1]\d)|(2[0-3])):[0-5]\d$")
        {
            ErrorMessage = "Fyll i datum och tid på formatet YYYY-MM-DD HH:mm";
        }
    }

This had no effect either! Still the same validation error. I concluded my validation is not run, maybe because the property is of type DateTime. I changed this to string. This, time I got no client side validation, but at least I got correct server side validation. My last attempt was to skip DataTimeValidationAttribute and use RegularExpressionAttribute directly.

        [Required(ErrorMessage = "Du måste fylla i från-datum och -tid")]
        [DataType(DataType.DateTime)]
        [RegularExpression(DateTimeRegExp, ErrorMessage = DateTimeError)]
        public string From { get; set; }

This was the only solution that worked for me.

Error “The connection string is not valid” after upgrading to .slqproj

Today I upgraded a couple of projects from .dbproj to .sqlproj using Visual Studio 2010.

First problem: One of the new projects did not build, saying it lacked master.dacpac. So I had to copy that file from “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\SQLDB\Extensions\SqlServer\100\SQLSchemas” to the “Master DB reference” folder within my project folder.

Second problem: When I checked in and the project was built using TFS/MSBuild, I got this error:

…\Binaries\Release\MyProjct.sql: The connection string is not valid

I didn’t want to publish directly to the database, because there is no network connection between our build server and the test server, but how do you turn that off? Unchecking Deploy for the project using solution configuration manager did not help.  After some binging, I found a clue. I opened up the project file using a text editor and changed

<Project DefaultTargets="Build;Deploy" xmlns="<a href="http://schemas.microsoft.com/developer/msbuild/2003">http://schemas.microsoft.com/developer/msbuild/2003</a>" ToolsVersion="4.0">

to

<Project DefaultTargets="Build" xmlns="<a href="http://schemas.microsoft.com/developer/msbuild/2003">http://schemas.microsoft.com/developer/msbuild/2003</a>" ToolsVersion="4.0">

That works. A file called MyProject_Create.sql is generated and I use that for deployment to the test server, where our application is reinstalled from scratch every night.