2

I have a code block in my unit test project as below

IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;

it produce error

Error   1   'System.Web.Mvc.ActionResult' does not contain a definition for 'Model' and no extension method 'Model' accepting a first argument of type 'System.Web.Mvc.ActionResult' could be found ..

How can I solve this?

2 Answers 2

2

If action returns view, cast it to ViewResult

((ViewResult)(IEnumerable<Product>)controller.List(2)).Model
Sign up to request clarification or add additional context in comments.

1 Comment

No need to cast it, just change the action to public ViewResult List(int param)
0

The answer of @archil will throw an exception at run time:

Unable to cast object of type 'System.Web.Mvc.ViewResult' to type 'System.Collections.Generic.IEnumerable`1[Product]'.

The correct cast is:

IEnumerable<Product> result = (IEnumerable<Product>)((ViewResult)controller.List(2)).Model;

Or simply, change the return type of the action to a ViewResult instead of an ActionResult:

public ViewResult List(int param)

And in the unit test use:

IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.