A
A
Anton Ivanov2020-12-05 21:43:40
ASP.NET
Anton Ivanov, 2020-12-05 21:43:40

How to cast model field type to int in asp.net?

There is a model that arrives in the request body in json format.
Some providers send the field as an int, others as a string (but the string is also a number, that is, parsing will be 100% successful)

You can somehow specify in asp.net that the field should be converted to int if it came as a string, or is it In any case, you need to write your own ModelBinder?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2020-12-05
@Morpheus_God

so what's stopping you from using int parse?

V
Vasily Bannikov, 2020-12-06
@vabka

If you use asp net core with Newtonsoft json, then by default such logic works there (I tried to turn it off myself, but I couldn’t)
There seems to be a separate option in System.Text.Json to enable / disable such logic, but this should be looked at in the documentation .
Well, you can always write your own converter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question