Piranha.core: Page Model Data returns Null after form submit

Created on 3 Jan 2021  路  5Comments  路  Source: PiranhaCMS/piranha.core

Hi, I have a custom page with a form. Everytime I click submit button, an exception thrown

System.NullReferenceException: 'Object reference not set to an instance of an object.'
Piranha.AspNetCore.Models.SinglePage.Data.get returned null.

I understand that form post method also refresh the page but why it does it return null in this case because I can refresh on browser without problem.

question

All 5 comments

The most common mistake is that you鈥檙e posting directly to the Razor Page or Controller and not to the permalink of the page. You need to post to the permalink for the middleware to kick in and the page data to be loaded properly.

Best regards

Thank you for your quickly reply!!!

Im using Razor Page. According to your above answer, I have modified <form action="@Model.Data.Permalink" method="post"> ... But it returns http error 400.

That's a good start! However if we look at the code for SinglePage<T> it automatically loads the model on a GET request, see here:

https://github.com/PiranhaCMS/piranha.core/blob/master/core/Piranha.AspNetCore/Models/SinglePage.cs#L60

You need to load the page model in your Post handler. See the post action in SinglePageWithComments<T> as an example:

https://github.com/PiranhaCMS/piranha.core/blob/master/core/Piranha.AspNetCore/Models/SinglePageWithComments.cs#L103

Best regards

Thank you so much!!! I have made it works like your suggestion.

Also, I would like to add an additional function to Piranha Manager just like Comment moderating. What should I do?

Hi there! We have a project template for creating a module with embedded manager interface that you can look at if you want to package your new manager views into it's own project. You can create a new module project by calling:

~
dotnet new piranha.module
~

assuming that you have our project templates installed.

Regards

Was this page helpful?
0 / 5 - 0 ratings

Related issues

w0ns88 picture w0ns88  路  6Comments

aneff-official picture aneff-official  路  4Comments

codesoroush picture codesoroush  路  6Comments

i-love-code picture i-love-code  路  3Comments

baibhavs picture baibhavs  路  3Comments