Search code examples
vb.netasp.net-mvc-4anonymous-typesrazor-2

Why does New <Type> With {} work but New With {} doesn't?


I've always used New With {} with no issues. Why is this different?

Razor VB.NET code

Using Ajax.BeginForm(New AjaxOptions With {.HttpMethod = "Post"})
    @* no problem *@
End Using
Using Ajax.BeginForm(New With {.HttpMethod = "Post"})
    @* Value of type '<anonymous type>' cannot be converted to 'System.Web.Mvc.Ajax.AjaxOptions'. *@
End Using

Solution

  • An anonymous type (New With {}) does not extend/inherit AjaxOptions - and an object conforming to AjaxOptions is required by BeginForm.