Mvc4 new {vs new object {

This is my first time learning MVC (MVC4) and completing the MvcMusicStore tutorial from http://www.asp.net/mvc

The tutorial is written for MVC 3 and when I write the following code (in my MVC4 project)

@Html.ActionLink(album.Title, "Details", new { id = album.AlbumId })

visual studio automatically inserts an “object” after entering new { to give me:

@Html.ActionLink(album.Title, "Details", new object{ id = album.AlbumId })

Is one of the methods more correct than the other, or is it a difference in MVC versions with more specific code?

+4
source share
1 answer

Syntax

 new object{ id = album.AlbumId } 

should give a compiler error, because "id" is not a property of the object.

Syntax

 new { id = album.AlbumId } 

is correct. It creates an anonymous type with id property

Note that in the view you will not get a compiler error at compile time (this may seem intuitive). The view is compiled at runtime. You may see a red error when there is an error in the view source code indicating a problem, but I found that it only works sometimes.

I saw the same problem as Visual Studio, introducing the wrong object after a new one.

+6
source

Source: https://habr.com/ru/post/1445601/


All Articles