tags:

views:

76

answers:

1

I have a controller with that I want to pass off some data to a view.

The view is strongly typed like:

System.Web.Mvc.ViewPage<SomeObject>

Now when I call the following in the controller:

return View("SomeAction", someObject);

I want it to enforce that I need to pass in 'someObject'.

Eg. I want the following to fail and not compile:

View("SomeAction");

No matter what, the expected object must always be passed to the view.

Is this possible or am I totally off base?

+1  A: 

I would just write a test for this case and don't bother for the rest:

var actual = controllerUnderTest.Action() as ViewResult;
Assert.IsNotNull(actual.ViewData.Model);
Assert.IsInstanceOfType(actual.ViewData.Model, typeof(SomeObject));
Darin Dimitrov
I would prefer that the compiler pick it up and possibly even the intellisense to help others instead of waiting for test cases to run to find the errors. That is the gist of what I am trying to get from the question.
Kelsey
As long as your controller derives from System.Web.Mvc.Controller nothing (at compile time) could prevent developers from calling the Controller.View("abc") method and NOT passing a model. What you could do instead is have your action return a CustomViewResult instead of ActionResult. This CustomViewResult could derive from ViewResult and "enforce" a model in a public constructor you define.
Darin Dimitrov