X
X
Xtone2019-06-12 20:50:13
ASP.NET
Xtone, 2019-06-12 20:50:13

How to solve problems with binding?

There is a model:

[Bind]
public class Person
{
  [ScaffoldColumn(false)]
  public int Id { get; set; }

  [Required]
  [Display(Name = "Фамилия")]
  public string LastName { get; set; }
  
  [Required]
  [Display(Name = "Имя")]
  public string FirstName { get; set; }
  
  [Required]
  [Display(Name = "Отчество")]
  public string MiddleName { get; set; }
  
  [Required]
  [Display(Name = "Возраст")]
  public int Age { get; set; }
}

There are two problems:
1. When receiving data from a request (for example, POST), despite required, it skips empty field values. More precisely, the fields are not specified at all. And it binds.
2. And when I make a GET request for a list of models of this type, on the contrary, I throw an exception because one of the required entries has a null field. But I don't need it to work on receiving - only on request.
PS By the way, I'm wondering how the request data type is determined? Empirically, I found out that the data must be sent in json so that the binding works and there is no error. But what if I wanted to use xml or form-data in general? Can this be specified somewhere?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question