Exception has been thrown by the target of an invocation in asp.net core and postgresql migration

I am trying to create a project. That’s why I downloaded a startup template from this link. Then I tried to configure with asp.net core. I set the connection string as "Default": "User ID=postgres;Password=**********;Host=localhost;Port=5432;Database=LibraryManagementDb;Pooling=true;". and then changed UseSqlServer to UseNpgsql in LibraryManagementDbContextConfigurer class.

Redirect back to Previous Page without losing original data

I have trouble preserving the original data by redirecting the same page when my custom error handling is executed in the controller.
Assume that I have a web page call Create.cshtml.
In that create webpage, I have a few form control which require the user to enter class code but the class code cannot be duplicated.
Assume that the user entered a class code that is existed in the system, my system should redirect back to Create.cshtml and pass error message (E.g. ViewBag.error = “Class Code duplicated”) and simulatenously .
But my current implementation does not revert back the original content/data after redirect.