How should I prevent ASP.NET core Model Binder from Binding properties defined in BLAZOE WASM Shared project(Model)

hamid Darab 21 Reputation points
2022-07-18T17:07:07.617+00:00

In my SPA application and on Server Side (Web Api), I need to exclude some properties from Model Binding.
Currently
1-it’s not possible to use BindNever or BindingBehavior(BindingBehavior.Never) Attributes in Blazor Webassembly Shared Project. which contains my EF Core Models.

2-On the other hand the the Exclude property is Removed from BindAttribute in dotnet.

I don't want to use Object mappers.
So how is it possible to Exclude some of my Model properties from ASP.NET Core Model Binding? (for example the Id property is Primary Key and generated Automatically by Database. I want the ModelStae.IsValide doesn't produce error when Id property is not found in Client-to-server json response)
Thanks

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,319 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 29,791 Reputation points Microsoft Vendor
    2022-07-20T07:30:44.153+00:00

    Hi @hamid Darab ,

    So how is it possible to Exclude some of my Model properties from ASP.NET Core Model Binding? (for example the Id property is Primary Key and generated Automatically by Database. I want the ModelStae.IsValide doesn't produce error when Id property is not found in Client-to-server json response)

    Agree with AgaveJoe, a ViewModel with the required properties is the easiest method.

    If you don't want to use this method, you could try to use a custom model binder to get the transfer data from the request body, then return a valid model to the API action method.

    Refer to the following article to use custom model binder:

    Custom Model Binding in ASP.NET Core

    Custom Model Binding In ASP.NET Core MVC


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments