Search code examples
asp.net-corerazorrazor-pages

razor: returned model is always null in Post


Say i have this form:

<form method="post" enctype="multipart/form-data" asp-page-handler="Button_Submit">
                    <input value="@Model.test"/>
                    <input style="width: 30% !important; opacity: 0.2; " value="Weiter" class="baseButton" type="submit">
                </form>

and this class:

namespace skillbased_webapp.Pages.Jobs
{
    [BindProperties]
    public class Skills : PageModel
    {
  
        public string test { get; set; } = "test";

        public void OnGet()
        {

        }
        public void OnPost([FromBody] Skills model)
        {

        }

    }

}

Then my page looks like this:

enter image description here

Its a simple input form with a submit button. If I now hit submit:

enter image description here

}

My model is null. There is no string on there. Nothing.

THe binding into the view works (as seen in the screenshot). But nothing is ever returned.

Why is that?


Solution

  • I am not sure why you receive the whole PageModel as parameter in OnPost() method, From your code, I think you just want to bind the value of test property, So you just need to change your view code like:

    <input value="@Model.test" asp-for="@Model.test" />
    

    Then in the backend, just:

    public void OnPost()
    {
    
    }
    

    The test property has bind the new value:

    strong text