Using Database Initializers and seeding

Add the following code in global.asax.cs

protected void Application_Start()     
        Database.SetInitializer(new MyStoreDbInitializer());

Create the following class

public class MyStoreDbInitializer : DropCreateDatabaseAlways<MyStoreDB>    
         protected override void Seed(MusicStoreDB context)
             context.Artists.Add(new Artist {Name = “Al Di Meola”});
             context.Genres.Add(new Genre { Name = “Jazz” });
             context.Albums.Add(new Album
                                     Artist = new Artist { Name=”Rush” },
                                     Genre = new Genre { Name=”Rock” },
                                     Price = 9.99m,
                                     Title = “Caravan”

How to load related object in Entity Framework

You can use the eager loading method show below to load the related objects:

public ViewResult Index()


 var products = db.Products.Include(a => a.Type).Include(a => a.Supplier);

 return View(products.ToList());



**Eager loading strategy attempts to load all data using a single query.




MVC3 - Using layout for views

You can specified view to use layout as following:


 Layout = “~/Views/Shared/_Layout.cshtml”;



Or you can override the _ViewStart.cshtml file within the view folder using the above.

MVC3 -Razor syntax sample

Implicit Code Expression


Explicit Code Expression


Unencoded Code Expression


Code Block


int x = 123;

string y = “because.”;


Combining Text and Markup

@foreach (var item in items) {

 <span>Item @item.Name.</span>


Mixing Code and Plain Text

@if (showMessage) {

 <text>This is plain text</text>



@if (showMessage) {

 @:This is plain text.


Escaping the Code Delimiter

My Twitter Handle is &#64;hacked


My Twitter Handle is @@haacked

Server Side Comment


 This is a multiline server side comment.

 @if (showMessage) {



 All of this is commented out.


Calling a Generic Method


Setting variables in JavaScript using JavaScript string encode

Use the @Ajax.JavaScriptStringEncode to encode the input.

<script type=”text/javascript”>
$(function () {
var message = ‘Hello @Ajax.JavaScriptStringEncode(ViewBag.Username)’;

jquery dropdownlist

Get value of the selected item:


Get text of the selected item:

$("#ComboBox option:selected").text()

On select value change:

$("#ComboBox").change(function() { /* do something here */ });

Add item to the list:

$(“<option value=’new item’>new item</option>”).appendTo(“#ComboBox”);


SQL Server 2008: "Saving changes is not permitted"

To change the Prevent saving changes that require the table re-creationoption, follow these steps:

  1. Open SQL Server Management Studio (SSMS).
  2. On the Tools menu, click Options.
  3. In the navigation pane of the Options window, click Designers.
  4. Select or clear the Prevent saving changes that require the table re-creation check box, and then click OK.

How to set default browser in Visual Studio

As it turns out setting this is quite simple.

  1. Open a WebForm file in VS (anything ending in will do) .aspx
  2. Select the option from the menu "Browse With..."File
  3. Select your preferred browser from the list and click the "Set as Default" button

ListControl AppendDataBoundItems Property

Here is a example:

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="country" DataValueField="country">

<asp:ListItem Text="(Select a Country)" Value="" />


You can setup an initial value -- such as an initial "(Select a Country)" that are not part of the databound result. Because the "AppendDataBoundItems" property is set to true, after databinding it will then have a dropdownlist whose first value is "(Select a Country)" followed by the data you bound to the list control.

IE script warning: If continues to run, your computer may become unresponsive.


IE getting an error when try to run a script that will take long time or need to loop through the page content.



Change the Web.config <compilation debug="true"> to "false" within the <system.web> section.