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.
- 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
1 Answer
Reset to default 1From 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
本文标签:
版权声明:本文标题:c# - Page has a binding model issue, along with some strange behaviour in the OnPost method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325510a2453645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论