Overposting: Binding attribute v ViewModel

Dean Everhart 1,496 Reputation points
2023-01-18T18:09:10.0833333+00:00

Is there a preference to methods to protect against over-posting between using the bind attribute or using a view model?

The tutorial refers to VIewmodels as an alternative to using the binding attribute but does not suggest that it is better...just an alternative way of doing it.

Re: Tutorial: [https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/controller-methods-views?view=aspnetcore-6.0#additional-resources

The [Bind] attribute is one way to protect against over-posting. You should only include properties in the [Bind] attribute that you want to change. For more information, see Protect your controller from over-posting. ViewModels provide an alternative approach to prevent over-posting.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,149 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Dean Everhart 1,496 Reputation points
    2023-02-01T18:53:17.36+00:00

    Perhaps I'm using the wrong spelling: I'm trying to use a view model (or viewmodel) as an intermediary between the view and the model. It would be located in a ViewModels folder at the following path: ProjectName.Models.ViewModels.

    My understanding is that it is a preferable way to bind data rather than the bind attribute in the Create and edit action methods / views.