Posting data from jquery ajax call to MVC4 controller: data posted is empty

Say you want to create a new contact from an ajax call and have somthing like that:

var theContact = {
lastname: 'Smith',
firstname: 'John'
var theData = JSON.stringify(theContact);
type: "POST",
url: "/contacts/create",
contentType: "application/json; charset=utf-8",
data: theData,
dataType: "json",
success: managesuccess

Then on the server, you carefully create your Contact class and the controller, paying attention to giving property names that are identical on client and server:

public class Contact
public string lastname;
public string firstname;

public string Create(Contact newContact)
// Logic

Only to realize that while the data is correctly posted from the client, on the server newContact properties stay desperately empty. Well, stop tearing you hair out, all you have to do is to follow best practice and adapt your Contact class by adding get/set! Like this:

public class Contact
public string lastname { get; set; }
public string firstname { get; set; }

And your data gets correclty posted, what a relief…

Error when adding a new MVC4 project to the solution

When trying to add a new MVC4 project to the solution, I received the following error message:

Cannot add the item because the item to add it to is not a solution folder or the solution


To fix this issue, go to tools/options, click Show all settings, and in projects and solutions, just check the “Always show solution” option. Now in the solution explorer, your project will appear under its parent solution. Then you will successfully add a new MVC4 project to the same solution.

Use SQL Express with your MVC4 Single Page Application

Currently playing with Visual Studio 2012 and MVC4, I’ll quickly share how to properly set up your membership provider to use SQL Server Express.

1. Install Visual Studio 11 beta

2. Install SQL Server Express 2012

3. Start Visual Studio, create a new project, select MVC4, then Single Page Application,

4. In SQL Server Management Studio, create a new database

5. Run aspnet_regsql.exe (in C:WindowsMicrosoft.NETFramework64v4.0.30319) and follow the steps. This will create the required tables in your new database to manage the different .Net providers (membership, profiles, roles, etc..).

6. In your application web.config, add a new connection string that points to your SQL Express. Should look like the following with SQL authentication:

<add name=”SQLExpressConnection” connectionString=”Data Source=.SQLExpress;Initial Catalog=mydatabase;User Id=sa;Password=password” providerName=”System.Data.SqlClient” />

And Windows authentication:

<add name=”SQLExpressConnection” connectionString=”Data Source=.SQLExpress;Initial Catalog=mydatabase;Integrated Security=true” providerName=”System.Data.SqlClient” />

7. Still in the web.config file, adapt the the DefaultMembershipProvider or add a new one in the <membership><providers> section. To actually have your application use your provider, make sure you set the defaultProdiver attribute of the mmbership node to the provider you want to use. Otherwise, the machine.config settings will be used. And make sure, in your provider, to set the connectionStringName to the connection string you defined above.


And you’re good to go!