Given the following controller class:
public class ProjectController : Controller { public ActionResult List() { return View(new List<string>()); } }
How can I get a reference to the model object in the following unit test?
public class ProjectControllerTests { private readonly ProjectController controller; public ProjectControllerTests() { controller = new ProjectController(); } [Fact] public void List_Action_Provides_ProjectCollection() { var result = (ViewResult)controller.List(); Assert.NotNull(result); } }
I have tried stepping into the controller action to see what internal fields were being set, but with no luck.
My knowledge of ASP.NET MVC is pretty limited, but my guess is that I am not setting up the controller with a correct context.
Any suggestions?
In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
cshtml file we need to right click on GetEmployeeInfo() Action Method & Click on Add View. From the Add View Window, give the View Name as GetEmployeeInfo, Template as Create, and Model class as EmployeeInfo. cs and click on Add, with this GetEmployeeInfo. cshtml file will be added in Views Folder.
Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, and press the Add button. This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.
Try:
result.ViewData.Model
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With