Model Binding in ASP.NET MVC

| Sania Uzair

ASP.NET is a free online framework for creating websites and applications using HTML, CSS, and JavaScript on the.NET Framework.

Converting HTTP request data (from the query string or form collection) to action method parameters is referred to as model binding. These parameters can be simple or complex in nature.

The user input is retrieved from the request object in the controller. We'll have to convert data types and map input values to properties. We must also perform data validation, such as reviewing data format and determining whether it is required. In controller, there are a lot of programmes to write. Model binding is provided by the ASP.NET MVC framework, which substantially simplifies the procedure. The framework retrieves the data, converts it to object properties, validates it, and passes the model object as a parameter to the controller.

Binding to Primitive Type:

The default model binder is capable of binding requests to basic types as controller parameters. In the example below, we get three values from the request.

Model Bindingin ASP.NET MVC

The default model binder can bind to a primitive data type collection. An array or a collection like IEnumerableT>, ICollectionT>, or IListT> can be used as the collection. Bindings of the type IDictionaryTKey, TValue> are also possible. The controller parameter for collection binding might look like this:

Model Bindingin ASP.NET MVC

To transmit parameters like the ones listed above to the controller, we must append index to the input name and identify it as a collection. We'll need html input that looks like this in an html form:

Model Bindingin ASP.NET MVC

Binding to Complex Objects:

In previous Student example, the object just has two simple properties, but we can add other object types to it and form a complex object graph. The default model binder can recursively traverse entire complex object graphs and populates nested property values. For example, we have following two classes:

Model Bindingin ASP.NET MVC

The controller may have signature like following:

Model Bindingin ASP.NET MVC

To bind nested properties, we will need to use dot notation to specify the complex type. The format is [ParentProperty].[Property]. The html input may look like following:

Model Bindingin ASP.NET MVC

Binding Attribute:

We can add binding attribute to instruct model binder when to bind property and when to exclude property. In the following example, we exclude StudentID in binding and include StudentName in binding.

Model Bindingin ASP.NET MVC

Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.

Chief Architect, Founder, and CEO - a Microsoft recognized Power Platform solution architect.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.

More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting