Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the differences between using Required and BindRequired in MvvmCross


What are the differences between using Required and BindRequired in MvvmCross


In MvvmCross, the terms Required and BindRequired are not directly applicable as they are in ASP.NET Core. However, understanding their roles in ASP.NET Core can help clarify how similar concepts might be applied in MvvmCross.

ASP.NET Core Context

- RequiredAttribute: This attribute ensures that a property is not null or empty after model binding has occurred. It is primarily used for validation purposes, ensuring that mandatory fields are filled in forms or data entry scenarios[1][3][6].

- BindRequiredAttribute: This attribute ensures that a property is present in the incoming data during model binding. Unlike Required, it checks for the presence of the property in the request data, not just its nullability[1][3][4].

MvvmCross Context

In MvvmCross, the focus is on data binding between the View and ViewModel. The framework does not use Required or BindRequired attributes directly. Instead, it relies on binding modes such as OneWay or TwoWay to manage data synchronization between the View and ViewModel[2][7].

- Binding Modes in MvvmCross:
- OneWay: Data flows from the ViewModel to the View.
- TwoWay: Data flows both ways between the View and ViewModel. This is the default mode for custom bindings in MvvmCross[7].

To achieve similar functionality to Required or BindRequired in MvvmCross, you would typically implement validation logic within your ViewModel or use custom bindings to ensure that certain properties are updated correctly. For example, you might use INotifyPropertyChanged to notify the View of changes in the ViewModel, ensuring that data is always up-to-date[2][8].

Implementing Validation in MvvmCross

While MvvmCross does not directly support Required or BindRequired, you can implement validation by checking for null or empty values within your ViewModel's properties. This can be done manually or by using validation frameworks that integrate with MvvmCross.

Summary

In summary, while Required and BindRequired are specific to ASP.NET Core for ensuring data presence and validation, MvvmCross relies on its binding mechanisms and custom validation logic to manage data integrity. If you need to ensure that certain data is present or valid in MvvmCross, you would typically implement this logic within your ViewModel or through custom bindings.

Citations:
[1] https://blog.dominikasikorska.pl/required-vs-bindrequired/
[2] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[3] https://dotnettutorials.net/lesson/bindnever-and-bindrequired-attribute-in-asp-net-core-mvc/
[4] https://www.strathweb.com/2017/12/required-and-bindrequired-in-asp-net-core-mvc/
[5] https://www.mvvmcross.com/documentation/plugins/methodbinding
[6] https://towardsdev.com/mastering-the-use-of-requiredattribute-a-detailed-comparison-of-required-vs-bindrequired-b3feaf108ef5
[7] https://stackoverflow.com/questions/41768962/mvvmcross-default-binding-mode-used-in-a-project
[8] https://www.mvvmcross.com/documentation/advanced/custom-data-binding