locked
How Do I call Multiple Tasks OnGet with Dependent Parameters on Razor Pages? RRS feed

  • Question

  • User-939035612 posted

    I am new to Razor pages and it looks to me like the closest thing to the Load event in a web form in OnGet in a Razor page. I can return the results on a async tasks from service classes unless one of those tasks depends on the results of the first. 

    Here is what I mean. Sorry if this make no sense but I am learning the .Net Core syntax and methodologies and trying to describe the result I am looking for.

    Here is my current Index Model

    public IndexModel(PostsService postService)
            {
                PostService = postService;
            }
            public PostsService PostService { get; }
            [BindProperty(SupportsGet = true)]
            public Posts Posts { get; set; }
            public IdentityUser IdentityUsers { get; set; }
            public object Context { get; private set; }
            public void OnGet()
            {
                int? id = Convert.ToInt32(RouteData.Values["id"]);
                Posts = PostService.GetSinglePost(id).Result;
                string userid = Posts.Userid;
                IdentityUsers = PostService.GetUserbyId(userid).Result;
            }

    This works great if I substitute Posts.Userid with the the userid in quotes "XXXX-XXXX-XXXX-XXXX" so I know that PostService.GetUserbyId(userid).Result works. The problem is that I get an object reference not set to an instance of an object error for Posts.Userid. It seems that Posts.Userid is not getting the userid from Posts even though it should get the Posts result before getting the Userid from those results. 

    In WebForms I could probably get around this by getting the Posts on PreRender, populating a hidden field with the userid, and then getting that on Load before getting the UserName, but I am not aware of Razor Pages having a pre-get or anything like that, so I am not really sure where to turn here because I need a nested get after the first get or something. Is there some way to tell the part of OnGet that id dependent on the first set of results to wait for those results before getting the userid?

    Monday, November 25, 2019 5:22 AM

Answers

  • User-939035612 posted

    That jarred my memory. I forgot to include userid in the posts select statement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 10:33 PM

All replies

  • User-474980206 posted

    webforms uses a control tree. oninit allowed you to code before the control tree was built, onload after the tree was built and post data applied, and pre-render just before the tree was converted to html. as razor pages does not use  control tree none of these events are required.

    the razor page code is the render, and on<action> fires before the render. if you are using OnGet, and you want to run code first, make it the first line. If you want to run after, make it the last line.  all post back data is bound before calling any razor page code. if the post back data is null, it was not included in the post data.

     

    Monday, November 25, 2019 4:15 PM
  • User-939035612 posted

    That jarred my memory. I forgot to include userid in the posts select statement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 10:33 PM
  • User711641945 posted

    Hi CopBlaster,

    CopBlaster

    I forgot to include userid in the posts select statement.

    So this is the reason why you do not get the userid in posts?If you have resolved the issue,could you mark your answer to help others?

    If not,could you share your `PostService.GetSinglePost()` method?

    Best Regards,

    Rena

    Tuesday, November 26, 2019 3:23 AM
  • User-939035612 posted

    I marked it. Turned out that I forgot to include the Userid in the GetPosts method

    Tuesday, November 26, 2019 9:38 PM