Corvo
Corvo

Reputation: 157

How to post to a Razor PageModel from partial view?

I'm building a website which has modal login and registrations forms.

The issue is when I'm trying to submit the form filled by the user, the OnPost of the corresponding razor models are not being called. I also tried using the Identity models as the models for the views and those are not being called as well.

Is there a way to do this correctly?

Upvotes: 0

Views: 736

Answers (1)

Anton Toshik
Anton Toshik

Reputation: 2909

In your Partial view you need to point the form to the page which has the OnPost handle.

<form asp-page="/YourPage" method="post">
     ...inputs...
     <button type="submit">Submit</button>
</form>

Make sure your OnPost accepts a a parameter of the view model like so:

OnPost(MyViewModel vm)

Realised you came after this question

Assuming you have the LoginViewModel as a property inside your razor page.

Instead of using

<partial name="_LoginPartial" 
      model='new LoginViewModel { InputModel = new InputModel() }' />

you want to use

<partial name="_LoginPartial" for='LoginViewModel' />

Upvotes: 1

Related Questions