Questions tagged [model-binding]
Model Binding is a feature of ASP.NET MVC, and (from .NET 4.5) ASP.NET WebForms, that simplifies controller actions by using the request data to create strongly typed objects.
model-binding
2,482 questions
0
votes
1
answer
117
views
Using [FromQuery] with a .NET controller for an optional boolean query string parameter where presence alone should set it
.NET has [FromQuery] to get query parameters on the path.
For instance my/foo?bar=1 maps to:
...
1
vote
1
answer
99
views
Accept enum parameter from HTTP header in controller action in ASP.NET Core
In my ASP.NET Core application, I want to expose an API endpoint that takes a single input parameter, which is of the following enum type:
...
4
votes
1
answer
144
views
Minimal API: Dictionary<string, string> always null when sending data with IFormFile via multipart/form-data
I’m using ASP.NET Core Minimal API. I have an endpoint where I want to receive an IFormFile along with a ...
0
votes
0
answers
85
views
ASP.NET MVC model binding FormData with arrays (with bracketed indexes)
Using model binding, ASP.NET MVC can take FormData from a form with repeating elements, signified by the use of indexes in square brackets, and populate a .NET ...
0
votes
1
answer
129
views
Model binding fails silently when using Razor pages (HTTP 400)
UPDATE: I changed the form a little bit and added some code to try and get the script to sync with the form. I also checked the payload and saw that only 2 out of 4 fields are being submitted to the ...
-1
votes
2
answers
199
views
ASP.NET Core Web API [NULL vs UNDEFINED]
In my HTTP controller using model binding, how can I determine if a request model property, is NULL because it was explicitly set to ...
0
votes
0
answers
67
views
How to globally reject null for non-nullable reference types in ASP.NET Core 9 Web API with System.Text.Json?
Background
I have an ASP.NET Core 9 Web API project with [nullable reference types] enabled (<Nullable>enable</Nullable>). In my request DTOs, I declare ...
0
votes
1
answer
110
views
entire DTO is null on invalid property value instead of just that property
I’m building a simple student ASP.NET Core 8.0 Web API and have a controller action that accepts a JSON body bound to a DTO. My DTO contains properties of various types, like int and ...
1
vote
1
answer
90
views
How to bind an axios request that contains an array of files to an ASP.NET Core model that contains a List<IFormFile> variable
I have started to take advantage of Axios header option multipart/form-data so I can go utilize Typescript objects for my requests to bind to my ASP.NET Core models,...
0
votes
2
answers
106
views
0
votes
1
answer
121
views
Razor Pages Binding a List<t> to HTML Table
I am using ASP.NET Core 8.0 with Razor Pages (not MVC). Webpage has a table where the prices of services offered can be modified as shown here:
I have created a model class ...
1
vote
1
answer
257
views
ASP.NET undesirable model binding issue - not getting all the data bound
Model binding in ASP.NET is not binding all the received data if the request contains the parmeter object name. I don't know how to describe it in a better way, but the sample I'm providing will make ...
0
votes
1
answer
206
views
How to bind in WinUI 3 nested template binding to the view model?
I have a winui3 app that has responsive UI for screen like desktop, tablet and mobile. I have three template for that. and I used content Control inside the page within the Content Control there is ...
0
votes
1
answer
57
views
Modelstate validation failed unexpectedly even with proper data passed from view
I have a simple viewmodel,
...
0
votes
1
answer
168
views
Facing a problem with model binding and handling object in my ASP.NET Core MVC application
I am facing a problem with model binding in my ASP.NET Core MVC application. Currently, the MVC template is the default one. When I write an HTTP POST method, after a user submits feedback, it is not ...
0
votes
1
answer
466
views
How to customize the error message returned when model binding fails due to a JSON deserialization exception?
I have a security problem in my app. When somebody sends wrong json to my endpoint (for example, pass type string instead if int), my API displays json deserialisation exception:
...
1
vote
0
answers
98
views
EF Core PostgreSQL jsonb column with POCO binding on nested model object
I have PostgreSQL server and a C# app that uses EF Core as an ORM.
This is the scenario I have - main model:
...
0
votes
0
answers
112
views
ASP.NET Core model attribute [FromForm] not binding
Given the following two model classes:
...
0
votes
1
answer
64
views
ASP.NET Core MVC : convert formatted currency string in front-end to decimal in controller using binding
I have an example class like this:
public class TestClass
{
[Precision(18,2)]
public decimal? Amount { get; set; }
}
In the frontend, ...
0
votes
1
answer
67
views
Custom IModelBinder: How to output only custom errors
I have the following method in a controller decorated with [ApiController] attribute:
...
0
votes
1
answer
71
views
ModelState doesn't see value
In my controller, I want to update FileContent in Image model. But ModelState says it's null.
...
1
vote
1
answer
91
views
How do I use ValidationAttribute or IModelBinder to ensure that an enum property exists in the JSON body and has a valid value?
Given a model that looks like this:
...
1
vote
1
answer
110
views
ASP.NET Core MVC: model binds all POSTed key values to a non-name matching property in addition to default model binding?
I am trying to get the raw POSTed key value pairs for additional processing logic later on in my action.
I have a class like this:
...
1
vote
1
answer
101
views
Cannot insert explicit value for identity column in table 'Products Discussed' when IDENTITY_INSERT is set to OFF
I get the error mentioned in the title.
Important
The form submits the data into the database successfully, but then it tries to submit again with another query that tries to simultaneously insert a ...
0
votes
1
answer
80
views
ASP.NET Core MVC: removing validation errors for non-posted values in model binding/json conversion?
By default when you post to an ASP.NET Core MVC method, the model binder/json converter will validate any validation attributes or custom validation logic on the full model when converting from JSON ...
3
votes
2
answers
735
views
Model Binding of dictionary in .NET 8 Minimal API
When someone calls my api endpoint (Minimal API), I would like to pull a dictionary from the query string.
I have the following query parameter struct for my Minimal API endpoint (simplified for this ...
0
votes
1
answer
103
views
Can't post decimal number in ASP.NET MVC
When I submit the form, the animal weight entered as a decimal is posted as a whole number.
4.5 or 4,5 comes to model as 45.
What can I try next?
This is the entity class:
...
1
vote
1
answer
24
views
ASP.NET Web Api Model Binding From A Stream
Im trying to send a http post request from Action1 to Action2 and instead of sending the payload as string Im sending it as stream for performance. Im getting the error.
Unsupported Media Type
...
0
votes
1
answer
70
views
How to bind nested FromForm data with List<IFormFile> to model form Javascript
I have a javascript form with a bunch of data that is appended to a form as follows:
...
0
votes
2
answers
85
views
I receive null after Model Binding
I receive variable with null values after model binding. What might be wrong here? Title is null, content is null after form submitting.
Create.cshtml
...
0
votes
1
answer
27
views
How to send some ids from the form to the controller using viewmodel
I have an index page in my .net mvc program, which has two parts for displaying contacts and adding contacts.
Contacts and groups in my database have a many-to-many relationship. I have been able to ...
0
votes
1
answer
59
views
ASP.NET Core MVC : model binding issues - view is passing null values to the action method of the controller
The form field property of my view is sending null values to the controller.
I have this view model:
...
0
votes
0
answers
60
views
How to create a custom model binder attribute
I am not sure if this is even possible but I have a model as follows:
...
1
vote
2
answers
1k
views
How to utilize FromBody and FromRoute in the same model with model binding
I am trying to utilize FromRoute for one property of a model and FromBody for the remaining items as the API is accepting JSON. My code is as follows:
...
0
votes
0
answers
245
views
Binding a nested object using FromForm
I have been able to properly bind a complex object to my model from a form but I am struggling for one item in particular and it is not making sense why it wont work as I have repeated the same logic ...
0
votes
0
answers
102
views
How to manually handle model deserialization errors similar to data annotations
I have previously been tasked with trying to submit json data and files to a web api shown here in this problem. The issue is I do not want to create a custom model binder for every instance this ...
1
vote
1
answer
180
views
Is there a way to make the ASP.NET Core model binder set properties with private setters?
I use ASP.NET Core minimal APIs and I try to have my view-models (where incoming client data gets mapped to) use private setters for their properties.
But I see that the binding stops working once I ...
0
votes
1
answer
367
views
C# XML ModelBinding - ASP.NET Core 8 Web API - required field not found
I want to bind this XML:
...
0
votes
1
answer
197
views
How to enable escaped JSON string parsing in asp.net core?
When I send a request to the endpoint with an escaped JSON string it throws an exception "JSON value could not be converted to Test.TestClass . Path: $ | LineNumber: 0 | BytePositionInLine: 90.\r\...
0
votes
1
answer
58
views
Custom ModelBinder for Interface .NET API
I'm trying to write a custom model binder for a .NET API Endpoint. I have a Post Endpoint with one parameter of type ICar. I ...
0
votes
1
answer
121
views
Model binding for ViewModel in ASP.NET Core MVC not working
When I send the model back to the Controller, the model instance returns with all properties null.
This is the ViewModel with some properties, including a SelectList and a model instance.
...
0
votes
1
answer
208
views
Are there any safer alternatives to hidden input fields for persisting properties across POST requests?
I have run into a problem where post requests in my CRUD app result in data loss due to simply forgetting to add a hidden field inside my <form> tags. For ...
-1
votes
1
answer
72
views
Custom Model binding with Collections
I have a List as a field in a Model class named "Person"
enter image description here
I have enabled Custom Person Model Binder globally in Program.cs file
enter image description here
Since ...
1
vote
3
answers
1k
views
ASP.NET Core pass Null parameters to controller endpoints
Using a model binder it is possible to pass a NULL complex type to a controller endpoint.
This solution does not work for all controller endpoints, rebuilding the model is also not straight forward.
...
0
votes
2
answers
757
views
Bind Entity Framework Core models to action method in ASP.NET Core controller
I have an ASP.NET Core controller with multiple action methods. Each action method has an {id} path parameter and do the following in almost every request:
...
0
votes
1
answer
1k
views
How to pass Model parameter value from an action to a view and back to another controller action?
How do I keep a Model alive across multiple actions and views? So that no parameter value is lost in between. I suspect I am doing some basic error in Model binding.
For example,
I have a Model
<...
0
votes
0
answers
106
views
wcf json array with variable name not deserializing
I have created a WCF service that accepts, for example, this json:
...
1
vote
1
answer
235
views
Bind multiple input files into IEnumerable
I am using ASP.NET MVC for a small website. I can't find a way to bind multiple file inputs into one IEnumerable.
I have the following inputs in my view:
...
0
votes
1
answer
274
views
ASP.NET Core's BindNever does not work with record
When using Razor Pages 7 I have this in the code-behind file:
...
2
votes
2
answers
5k
views
How to accept all content-types on my ASP.NET Core Web API?
I have an endpoint on my ASP.NET Core Web API that looks something like this:
...