Copied to clipboard

Flag this post as spam?

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


  • hakan 24 posts 118 karma points
    Jun 16, 2020 @ 22:11
    hakan
    0

    can I use controller before render a page?

    I want to fetch some data from another DB (not umbraco DB) in controller and pass this data to cshtml (umbraco page). is it possible?

  • Peter van den Dungen 66 posts 365 karma points
    Jun 17, 2020 @ 07:06
    Peter van den Dungen
    1

    Yes, I think its very simple what you want to do.

    1. Just add a second connectionString in your web.config, with the settings to connect to the other database.
    2. Then I would use something like EF to create a DbContext.
    3. Load data from this db in the controller, save the data in a viewmodel object and pass it back to the view.
  • hakan 24 posts 118 karma points
    Jun 17, 2020 @ 09:31
    hakan
    0

    if I send a request with ajax to my controller I can get data ajax success. your write is right. but I cant enter controller before render cshtml. I didnt find any doc. about this. I want to use umbraco like MVC logic. firstly I prepare data in controller after that I will pass data to cshtml

  • Peter van den Dungen 66 posts 365 karma points
    Jun 17, 2020 @ 09:43
    Peter van den Dungen
    0

    Umbraco is build on MVC, so every possible thing in MVC can be done in Umbraco as well.

    Did you inherit UmbracoApiController on your Controller?

    This topic could be intresting for you: https://our.umbraco.com/forum/developers/api-questions/45776-Calling-Surface-Controller-From-Ajax

  • Aaron 58 posts 406 karma points MVP 2x c-trib
    Jun 18, 2020 @ 22:14
    Aaron
    100

    Have you looked at render controllers?

    I use these to create a model before the main view has loaded, and then surface controllers to control the partials

    https://our.umbraco.com/documentation/reference/routing/custom-controllers#example-hijacking-route-requests-to-a-product-page

  • Dhanesh Kumar MJ 165 posts 521 karma points MVP c-trib
    Jun 19, 2020 @ 04:56
    Dhanesh Kumar MJ
    1

    Hey Hakan,

    You can achieve this in two way's

    1) By writing an API using SurfaceController and call the API.

    2) Use Umbraco Route Hijacking / Custom controller Please see here Controller

    Example

    Regards Dhanesh :)

  • hakan 24 posts 118 karma points
    Jun 19, 2020 @ 10:35
    hakan
    0

    thanks aaron and Regards. "Route Hijacking / Custom controller" is answer of my question I asked exactly.

Please Sign in or register to post replies

Write your reply to:

Draft