mvc4 new { vs new object{

asp.net-mvc

Solution

The syntax

new object{ id = album.AlbumId }

should yield a compiler error because "id" is not a property of object.

The syntax

new { id = album.AlbumId }

is correct. It creates an anonymous type with a property called id

Note that in a view, you will not get a compiler error at compile time (counter-intuitive as that may seem). The view is compiled at runtime. You may see a red squiggle under the error in the view's source code indicating a problem, but I have found that to work only sometimes.

I have seen the same problem with Visual Studio injecting an incorrect object after new.

Problem

I am learning MVC (MVC4) for the first time and completing the MvcMusicStore tutorial from http://www.asp.net/mvc The tutorial is written for MVC 3 and as I am writing the following code (in my MVC4 project) `@Html.ActionLink(album.Title, "Details", new { id = album.AlbumId })` visual studio is automatically inserting 'object' after I type `new {` to give me: `@Html.ActionLink(album.Title, "Details", new object{ id = album.AlbumId })` Is one way more correct than the other or is it a difference in MVC versions to have more defined code?

Original source