views:

148

answers:

2

What are the possible reasons what Nullable<> types are disallowed to be passed as TModel parameter of System.Web.Mvc.ViewPage<TModel> generic? This could be handy sometimes.

In ASP.NET MVC source defined what TModel should be a class:

public class ViewPage<TModel> : ViewPage where TModel : class

but Nullable types are value types. Maybe definition could be less restrictive...

+1  A: 

A single value isn't really a "model" - you could just use the dictionary and cast?

Marc Gravell
+3  A: 

It wouldn't make sense for this to work at the moment, as the ViewPage<T> internally always verifies that the model is an instance of T. You'd never be able to pass null because of this check.

As for why structure types weren't allowed in the first place, there were a multitude of reasons. Among them: (a) it might encourage people to use custom structure types for a model, which is almost never the correct thing to do; (b) you can't pass a structure type as a parameter to Controller.UpdateModel(); (c) models should have reference equality in order that filters can inspect and modify them; (d) the built-in structure types like int, etc., normally aren't useful by themselves as your model object; and (e) structure types don't support inheritance, e.g. passing an int model into a ViewPage<long> would blow up.

It turns out that (c) probably isn't a problem because structure types should be immutable, but the other items require that allowing structure types be given ample thought and design decision before supporting them. The consequences of supporting these are much greater than just removing the constraint on ViewPage<T>.

Levi