admin管理员组

文章数量:1333736

Pref: I experienced some really strange behavior in ASP.NET Core. I started a MVC project, but I also included some Razor pages in the project.

When I attempt to bind the model and click the "Save" button, the input values are null or empty. The model is invalid and does not include the input values, it is completely empty. I checked all of the properties, and everything seems almost correct, but it is not binding the input values for Product.

[BindProperty]
public ProductViewModel Product { get; set; }

It's really strange that I created a second button, but it also calls the OnPost method for some reason... Similar case: removing the OnPost method and creating a method named OnPostSave does not get called, something went really wrong

<form method="post">
    @Html.AntiFeryToken()
    <input asp-for="Product.Name" class="form-control" type="text" />
    <button type="submit" asp-page-handler="Save" class="btn btn-primary">Test</button>
</form>

I suspect there may be an issue with the routing or a misconfiguration, but I'm not entirely sure. If you have any ideas or need more information, please let me know.

Thank you!

I attempted to debug the OnPost method, but I always received null or empty values. In another case regarding the button click event, I tried renaming the method and adding some validation, but it was unsuccessful.

Pref: I experienced some really strange behavior in ASP.NET Core. I started a MVC project, but I also included some Razor pages in the project.

When I attempt to bind the model and click the "Save" button, the input values are null or empty. The model is invalid and does not include the input values, it is completely empty. I checked all of the properties, and everything seems almost correct, but it is not binding the input values for Product.

[BindProperty]
public ProductViewModel Product { get; set; }

It's really strange that I created a second button, but it also calls the OnPost method for some reason... Similar case: removing the OnPost method and creating a method named OnPostSave does not get called, something went really wrong

<form method="post">
    @Html.AntiFeryToken()
    <input asp-for="Product.Name" class="form-control" type="text" />
    <button type="submit" asp-page-handler="Save" class="btn btn-primary">Test</button>
</form>

I suspect there may be an issue with the routing or a misconfiguration, but I'm not entirely sure. If you have any ideas or need more information, please let me know.

Thank you!

I attempted to debug the OnPost method, but I always received null or empty values. In another case regarding the button click event, I tried renaming the method and adding some validation, but it was unsuccessful.

Share Improve this question edited Nov 21, 2024 at 10:27 Smeth asked Nov 20, 2024 at 22:03 SmethSmeth 255 bronze badges 2
  • 1 Code is text and should be included in the question as text. MVC binding will either populate a function parameter, or a model property. Why are you trying to do both? – Jeremy Lakeman Commented Nov 21, 2024 at 1:33
  • This is just a test, as almost nothing is worked, that's why. – Smeth Commented Nov 21, 2024 at 9:09
Add a comment  | 

1 Answer 1

Reset to default 1

From your shared screenshot, it seems the tag helper does not work. You can F12 in the browser and click the Elements tab to check the generated html, the correct html should be like:

<input type="text" id="Product_Name" name="Product.Name" value="">

Please add the following code in the top of your current Razor Pages:

@page
@model IndexModel

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Or globally add it in Pages/_ViewImports.cshtml:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Reference:

Managing Tag Helper scope

本文标签: