Home > OS >  How to use ViewModels from another project in ASP.NET MVC Core
How to use ViewModels from another project in ASP.NET MVC Core

Time:09-28

During trying out Razor I moved my view models and controllers to separate project and switched to nuget library Microsoft.AspNetCore.Mvc.Core instead Microsoft.AspNetCore.Mvc, so now i have base class for controllers ControllerBase and not Controller and I don't have method View(object viewModel) to return and setup view model for a page.

Now I have methods like Ok(object data) which just then returns JSON to my page, and instead of rendering the view, i get raw JSON.

So the question is how to setup View Model for a page properly in this case?

CodePudding user response:

For clearer view, I switched away from Microsoft.AspNetCore.Mvc as it was not fully compatible with .NET Core and later, so I had to search for equivalent in NET Core world.

After comment to my question I realized I can't use ControllerBase, while it was very temtping and easy and almost worked.

But then I found (or VS suggested :P ) nuget Microsoft.AspNetCore.Mvc.ViewFeatures which had Controller abstract class that contains desired method View(object viewModel) that works correctly.

  • Related