Monday, 5 May 2014

ASP.NET MVC Versions and Features

ASP.NET MVC1

Released in Mar 2009, Runs on .Net 3.5 / Visual Studio 2008, WebForm View Engine, Html Helpers, Ajax Helpers, Routing, Unit Testing

ASP.NET MVC2

Released in Mar 2010, Runs on .Net 3.5 / Visual Studio 2008 & .Net 4.0 / Visual Studio 2010, Strongly typed Html Helpers i.e. lambda expression based Html Helpers, Data Annotation Attributes, Client-side validation, UI helpers with automatic scaffolding & customizable templates, Attribute-based model validation on both client and server, Asynchronous controllers

ASP.NET MVC3

Released in Jan 2011, Runs on .Net 4.0 / Visual Studio 2010, Razor view engine, ViewBag dynamic property for passing data from controller to view, Global Action Filters, Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding, Improved Support for Data Annotations, Remote Validation, Compare Attribute, Partial-page output caching, Sessionless Controller, Child Action Output Caching, Dependency Resolver, Entity Framework Code First support, Use of NuGet to deliver software and manage dependencies throughout the platform, Good Intellisense support for Razor into Visual Studio

ASP.NET MVC4

Released in Aug 2012, Runs on .Net 4.0, 4.5 / Visual Studio 2012, ASP.NET Web API, Enhancements to default project templates, Mobile project template using jQuery Mobile, Display Modes, Task support for Asynchronous Controllers, Bundling and minification, Support for the Windows Azure SDK

ASP.NET MVC5

Released in Oct 2013, Runs on .Net 4.5, 4.5.1 / Visual Studio 2013, ASP.NET Web API2, One Asp.Net, Asp.Net Identity, ASP.NET Scaffolding, Authentication filters - run prior to authorization filters in the ASP.NET MVC pipeline, Bootstrap in the MVC template

Click here to read about C# Versions and features

Using ViewBag in _ViewStart.cshtml

Trying to set or get some value to/from ViewBag in _ViewStart.cshtml will throw an error when you run the app. There is a workaround for this using ViewContext.Controller.ViewBag.

Here is some sample code

ViewContext.Controller.ViewBag.StoreName = "My New Store";

...

@ViewContext.Controller.ViewBag.StoreName

Read more >>

ViewData mechanics and segmentation

Wednesday, 2 April 2014

what is the purpose of _viewstart.cshtml in ASP.NET MVC?

The _ViewStart.cshtml file will execute at the start of each view's rendering. Any code contained within the code block in this file will execute before any code in the view. Typically, this file will set the layout template to be used by the views in the application:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. So we can optionally make our Layout selection logic richer than just a basic property set,if required.

Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of our individual view files (except if we wanted to override the default value above).

An MVC application can have multiple _ViewStart.cshtml files. The order in which these files execute is dependent upon the location of the files in the folder hierarchy and the particular view being rendered. The MVC Runtime will first execute the code in the _ViewStart.cshtml file located in the root of the Views folder. It will then work its way up the folder hierarchy, executing the code in each _ViewStart.cshtml file it finds along the way. For example, if we had_ViewStart.cshtml files in the following locations:

1) Views\_ViewStart.cshtml
2) Views\Home\_ViewStart.cshtml
3) Views\Products\_ViewStart.cshtml

When a view in the Views\Home folder is rendered, the MVC Runtime will first execute the code in file 1 and then execute the code in file 2. Since file 3 is not in the hierarchy of the Views\Home folder, the code in file 3 will not get executed.

Visual Studio 2012 - class names lost default colour and appear in black

Recently some settings on my Visual Studio 2012 got changed somehow and I lost most of the default colour settings in the IDE text editor for C#. Also "User Types" was missing from "Display items" list of "Tools > Options > Environment > Fonts and Colors" dialog box. So I was unable change the colour manually. I managed to bring back the default settings by doing the following.

  1. Delete folders
    • %appdata%\Roaming\Microsoft\Microsoft Visual Studio
    • %appdata%\Roaming\Microsoft\VisualStudio
    • %appdata%\Local\Microsoft\VisualStudio
    • My Documents/Visual Studio 2012/Settings
  2. Open Visual Studio 2012 command prompt (as administrator) and run
    • devenv.exe /setup
    • devenv.exe /ResetSettings
    *devenv.exe location is usually something like C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE.
I think the first step is irrelevant. So if you experience this problem I would suggest you skip step#1 and just follow step#2.

References

no-intellisense-no-indentation

class-names-not-coloured-in-visual-studio

how-can-i-get-user-type-c-sharp-syntax-highlighting-working-again

Monday, 31 March 2014

Passing date value as a string to JavaScript Date() object

Date() Constructor has following variations

new Date();
new Date(value);
new Date(dateString);
new Date(year, month [, day, hour, minute, second, millisecond]);

Date constructor parameters

Note: Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g.  new Date(2013,13,1) is equivalent to new Date(2014,1,1), both create a date for 2014-02-01 (note that the month is 0-based).
value

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.

dateString

String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). Date.parse()

The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

Date.parse() understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed.

<script>
alert(new Date("March 18, 2014"))
alert(new Date("March 18, 2014 00:00:00 +0430"))
alert(new Date("18 March 2014"))
alert(new Date("2014-03-18"))
</script>

year

Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999.

month

Integer value representing the month, beginning with 0 for January to 11 for December.

day

Integer value representing the day of the month.

More info

Date Object

Date.parse

JavaScript Dates

Wednesday, 26 March 2014

CSS3 word-break and word-wrap properties

word-break

Normally, line breaks in text can only occur in certain spaces, like when there is literally a space or a hyphen. When you set word-break to break-all, line breaks can occur between any character. Resulting in, for example:

Perform
ance

(if that word wouldn't quite fit into its parent container width)

Syntax
  word-break: normal | break-all | keep-all;
Example
p {
  word-break: break-all;
}

It's often used in places with user generated content so that long strings don't risk breaking layout. One very common example is a long copy and pasted URL. If that URL has no hyphens, it can extend beyond the parent box and look bad or worse, cause layout problems.

Read about word-break on w3schools.com

Read about word-break on css-tricks.com

word-wrap

Allow long words to be able to break and wrap onto the next line:

Syntax
word-wrap: normal|break-word|initial|inherit;
p.test {word-wrap:break-word;}

Read about word-wrap on w3schools.com

Friday, 21 March 2014

What is the difference between DIV and SPAN?

div is a block element, span is inline. This means that to use them semantically, divs should be used to wrap sections of a document, while spans should be used to wrap small portions of text, images, etc.