DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Mapping asp.net request data to controller action simple parameter types

In ASP.NET, you can map incoming request data to controller action parameters in several ways, depending on the parameter type. Here are some common parameter types and how you can map them:

  1. Simple Types (e.g., int, string): For simple types, ASP.NET automatically maps query string or form data values with matching parameter names. For example, if your action method has a parameter named "id" of type int, ASP.NET will automatically map the value of "id" from the query string or form data to that parameter.

Example:

   public IActionResult MyAction(int id)
   {
       // Use the 'id' parameter value
       return View();
   }
Enter fullscreen mode Exit fullscreen mode
  1. Model Binding: ASP.NET provides model binding to automatically map complex types from request data. Model binding can be used for parameters of custom types or complex objects. It maps request data based on parameter names and property names in the object.

Example:

   public IActionResult MyAction(MyModel model)
   {
       // Use properties of the 'model' object
       return View();
   }
Enter fullscreen mode Exit fullscreen mode
  1. FromQuery Attribute: You can use the [FromQuery] attribute to explicitly indicate that a parameter should be bound from the query string, even for simple types.

Example:

   public IActionResult MyAction([FromQuery] string name)
   {
       // Use the 'name' parameter value from the query string
       return View();
   }
Enter fullscreen mode Exit fullscreen mode
  1. FromForm Attribute: The [FromForm] attribute allows you to explicitly indicate that a parameter should be bound from form data, even for simple types.

Example:

   public IActionResult MyAction([FromForm] int age)
   {
       // Use the 'age' parameter value from the form data
       return View();
   }
Enter fullscreen mode Exit fullscreen mode

These are some common ways to map request data to controller action parameters in ASP.NET. You can choose the appropriate method based on the parameter type and the source of the data (query string, form data, etc.).

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay