Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Hursan 4 posts 64 karma points
    Dec 14, 2023 @ 14:50
    Daniel Hursan
    0

    Display simple view with a simple object

    Hello, this is my 3rd day in a row trying various stuff from internet to accomplish something that would take 10 minutes in the old MVC approach.

    I'm very confused on the multiple ways of using controllers (RenderController, SurfaceController, UmbracoPageController, ViewComponents).

    In old MVC, I could have a Controller with multiple actions, [HttpGet], [HttpPost], prepare a view model and feed it to the view.

    Right now, it's been a painful experience with Umbraco way of doing things

    1. I'm forced to declare document types that match my controller names, otherwise it won't work with route hijackling
    2. I must extend SurfaceController, or post requests won't work
    3. I can't have even the simplest get method (other than Index), to display a view with an object Keep getting this:

      ModelBindingException: Cannot bind source type Orlando.BusinessObjects.Vehicle to model type Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent

    4. In order to use non-Index action, I have to create custom routes, and even then, I'll still get the same error as above

      Is it really this bad to work with or I can't understand the basics of it? How can I shift the MVC mindset I'm accustomed to, to the way Umbraco works?

    For example, the view part

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @using Orlando.BusinessObjects;
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    
    // If I comment this line below and try @model Vehicle, same problem
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Vehicle>
    
      @{
            Layout = "master.cshtml";
            var vehicle = Model;
        }
    
    <div class="container-fluid p-0">
    .....
    </div>
    

    Controller:

        public class InventoryDetailController : UmbracoPageController
    {
    
      private readonly ILogger<InventoryDetailController> _logger;
      private readonly IMyAPIService _myAPIService;
    
      public InventoryDetailController(
        ILogger<InventoryDetailController> logger,
        ICompositeViewEngine compositeViewEngine,
        IMyAPIService myAPIService)
        : base(logger, compositeViewEngine)
      {
        _myAPIService = myAPIService;
        _logger = logger;
      }
    
      public async Task<IActionResult> Detail(Guid vehicleId, CancellationToken token = default)
      {
        var vehicle = await _myAPIService.GetVehicleByIdAsync(vehicleId.ToString());
        return View("~/Views/InventoryDetail.cshtml", vehicle);
      }
    }
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Dec 14, 2023 @ 15:33
Please Sign in or register to post replies

Write your reply to:

Draft