如何使用Razor页面解开属性
我使用剃刀页。
我在onpost
方法中都有此属性
[BindProperty]
public ProductDto CreateProduct { get; set; }
,我检查modelState.isvalid
,一切都可以。
但是,当我向我的处理程序发送请求时,我的处理程序OnPost
方法验证将为false。
原因是modelState
检查我的处理程序输入,还createProduct
使用bind bind属性属性,当我发送bindproperty
属性时,我如何解开属性请求汉德斯。
public IActionResult OnPostAddBrand(AddBrandDto model)
{
if (!ModelState.IsValid)
{
// AddBrandDto is valid but I got here.
Return Json(false);
}
// todo: SaveBrand
Return Json(true);
}
我知道,如果我不使用bindproperty
属性并从方法输入中获取对象,则将解决问题,例如:
public ProductDto CreateProduct { get; set; }
public async Task<IActionResult> OnPost(ProductDto createProduct)
{
}
但是还有其他方法可以解决此问题。
I use razor pages.
I have this property
[BindProperty]
public ProductDto CreateProduct { get; set; }
In OnPost
Method, I check ModelState.IsValid
and everything is ok.
But when I send a request to my handlers that are next to my OnPost
method the validation will be false.
The reason is ModelState
check my handler inputs and also CreateProduct
that used bind property attribute, how can I unbind property that use BindProperty
attribute when I send request to the hanlders.
public IActionResult OnPostAddBrand(AddBrandDto model)
{
if (!ModelState.IsValid)
{
// AddBrandDto is valid but I got here.
Return Json(false);
}
// todo: SaveBrand
Return Json(true);
}
I know if I don't use BindProperty
attribute and get the object from the method inputs, problem will be solved, like this:
public ProductDto CreateProduct { get; set; }
public async Task<IActionResult> OnPost(ProductDto createProduct)
{
}
But is there any other way to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用要从
modelstateDictionary
中删除属性
You can use ModelState.Remove to remove properties from the
ModelStateDictionary
e.g.If you want to "unbind" a complex property, you can use reflection to remove its properties: