ASP.Net Razor page validation not returning values

user8906214
    public IActionResult OnPost() {
        if (ModelState.IsValid) {
            \\ save it
        }
        return Page();
    }

In my razor code, I've got the above checking if the model is valid. This works fine, if the model is valid, it saves, if not it goes to return Page(); which is what all the documentation I can find says to do.

The problem is that if the model is invalid, the return Page(); reloads the page so I've lost all the entered values and none of the validation messages are loaded from the model.

What should I use instead of return Page(); to display the validation messages and keep the invalid information ?

------------------- Edit with ViewModel

So with this as my OnPost

[BindProperty]
public theVM.ModelinVM theVMInstance { get; set; }
public IActionResult OnPost() {
    if (ModelState.IsValid) {
        \\ save it
    }
    return theVMInstance;
}

and this is the view model

public class theVM {
    public IQueryable<originalModel> ModelinVM { get; set; }

}

return theVMInstance; now says Cannot implicitly convert type 'theVM.ModelinVM' to 'Microsoft.AspNetCore.Mvc.IActionResult'.

It's expecting an IActionResult so doesn't it have to be compatible with that?

user8906214

It didn't work because I hadn't put

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

at the top of the .HTML page.

I didn't need the OnPost() method, the jQuery did th ejob for me.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Asp.net Core, set multiple selected values in Razor Page

ASP.NET Core 2.2 Razor Pages - AJAX Call to page model not returning data

ASP.NET Core Razor Pages - Complex model property is null after returning Page()

asp.NET Razor new Page error

ASP.net razor page checkbox not binded

PageFilter in asp.net core razor page

JsonResult not returning data after Ajax call in .NET Core Razor Page

Fluent Validation in Razor Page

ASP.Net MVC validation with Angular VS Razor

Validation in ASP.NET Web Pages (Razor) deprecated?

ASP.Net Razor MVC Bootstrap Login Modal Validation

ASP.net request validation & Razor — do I really need it?

Remote Validation in Asp.Net Core Razor Pages

Modal form validation in asp.net core 2.1 and Razor pages

Changing the default landing page in ASP.NET Core Razor page?

ASP.NET Core 2.1 Razor page return Page with model

ASP.NET Core 2.0 read: Options in razor page

React MaterialUI hosted in ASP.NET Razor Page

ASP .NET Razor v3 page never finishes loading

ASP.NET Razor Pages equivalent of a Page load method

Unterminated string constant error in ASP.NET Razor page

Binding radio buttons in ASP.NET MVC page with razor

Partial razor page won't render in ASP .NET Core

HTTP 400 on simple ASP.Net Core Razor page

Custom Route to About Razor Page in ASP.Net Core 2.1

How to reuse HTML snippets in asp.net core razor page?

use icon in ActionLink for ASP.NET MVC, Razor Page

Hooking into razor page execution for ASP.Net Core

Example AJAX call back to an ASP.NET Core Razor Page