1

I would like to render view component inside pagemodel in onget method. When i try to execute the page I always get NULL REFERENCE ERROR. Can anybody help me with it. Here is the code i am using.

  private readonly IViewComponentHelper _helper;

    public MyPage( IViewComponentHelper helper)
    {
        _helper = helper;
    }

    public async Task<ActionResult> OnGet( string id,string type)
    {

        var result = await _helper.InvokeAsync(
                         "mycomponent",
                         new
                             {
                                 id= id,
                                 type = type
                             });
     }
Narendra
  • 1,332
  • 2
  • 13
  • 16
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mustafa Gursel Jul 07 '19 at 18:29
  • Can you provide the exception stack trace? – Ammar Jul 08 '19 at 07:12

1 Answers1

0

Try this and see if it works.

  1. Define a ViewContext property in your page
[ViewContext]
public ViewContext ViewContext { get; set; }
  1. Right before you call _helper.InvokeAsync, add this line of code
((IViewContextAware)_helper).Contextualize(ViewContext);

Hope this helps!

Ray
  • 12,101
  • 27
  • 95
  • 137