I
I
Ihaarr2021-06-29 11:32:52
ASP.NET
Ihaarr, 2021-06-29 11:32:52

Why don't all required fields get validated?

There is an entity:
public class Products
{
public int Id { get; set; }
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
[Required(AllowEmptyStrings = false)]
public string Description { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime Created_at { get; set; }
[Required]
public int In_stock { get; set; }
[Required]
public decimal Price { get; set; }
[Required]
public int External_id { get; set; }
}
POST request, I pass json in the body
In the controller public ActionResult CreateProduct(Products product)
I check
if (ModelState.IsValid)
If there is no Name or description field, then an error is returned.
If name, description are present, but others are not, then everything is OK. Why is that?
I assume that when a product is created, then the fields (which are not string) are filled with zeros and supposedly this is ok. If so, how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-06-29
@Ihaarr

Make fields nullable or implement IValidatableObject.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question