Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get model data from a ViewResult in ASP.NET MVC RC1?

Tags:

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?

like image 507
Arnold Zokas Avatar asked Feb 05 '09 22:02

Arnold Zokas


People also ask

How do I access model data from a controller?

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.

How pass data from controller model in ASP NET MVC?

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.

How do you call a model in Cshtml?

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.

How do I access my razor view model?

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.


1 Answers

Try:

result.ViewData.Model 

Hope this helps.

like image 145
Florin Sabau Avatar answered Oct 14 '22 15:10

Florin Sabau