0

i have the text field 'fio' in my search form, which can be empty.

home.html:

<dx-text-box
          [(value)]="fio"         
        ></dx-text-box>

this is my component and service to send the search parametres for API

component:

public fio!: string;


public updateFromPeriod(): void {
  
    this.updateService.updateFromPeriod(this.DateIn, this.DateOut, this.punktsID(this.punktValue), this.fio).subscribe({
        next: value => {
      
          this.EventLogs = value;
        },
        error: err => {
          console.error(err);
        }
    });

service:

updateFromPeriod(DateIn: any,
        DateOut: any,
        idPunkts: any,
        fio: string) {
            const options = {
                params: new HttpParams().append('DateIn', this.toISOLocal(DateIn)).append('DateOut', this.toISOLocal(DateOut)).appendAll({'idPunkts': idPunkts})
                .append('fio', fio),
                headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
              };
              console.log('PARAMS: ' + options.params);
      return this.httpClient.get<EventLog[]>(this.BaseUrl +'EventLog/FromPeriod', options);
    }

and server method:

public IEnumerable<EventLog> Get(
        [FromQuery] DateTime DateIn, 
        [FromQuery] DateTime DateOut, 
        [FromQuery] int[] idPunkts, 
        [FromQuery] string fio)         {             
    List<int> punkts = idPunkts.ToList();             
    var login = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;             
    return EventLogRepository.GetEventLog(DateIn, DateOut, punkts, fio, login);
}

I want to make the fio parameter optional in query string, which will not send, if it's empty. And the query string will be like this

http://localhost:5025/EventLog/FromPeriod?DateIn=2023-02-08T11%3A50%3A10.198Z&DateOut=2023-02-08T11%3A50%3A10.198Z&idPunkts=18

i try to add ?fio: string in my service method, but have the type mistake. Als i tried to add null on api controller, but it still doestn work.

ShayD
  • 689
  • 7
  • 17

1 Answers1

0

you should conditionally append it to your params:

let params:HttpParams = new HttpParams().append('DateIn', this.toISOLocal(DateIn)).append('DateOut', this.toISOLocal(DateOut)).appendAll({'idPunkts': idPunkts});
if(fio){ params = params.append('fio', fio);}
const options = {
            params: params,
            headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
          };

There is probably more elegant way but that's the basic principle.

ShayD
  • 689
  • 7
  • 17
  • it's still not working, message in the console says ""The fio field is required."" – CreatureFear Feb 09 '23 at 08:44
  • It seem like your api requiring the field. Can you share your server code? – ShayD Feb 09 '23 at 10:57
  • yes, this is api method `public IEnumerable Get([FromQuery] DateTime DateIn, [FromQuery] DateTime DateOut, [FromQuery] int[] idPunkts, [FromQuery] string fio) { List punkts = idPunkts.ToList(); var login = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value; return EventLogRepository.GetEventLog(DateIn, DateOut, punkts, fio, login); } ` – CreatureFear Feb 09 '23 at 11:24
  • Make fio optional by setting its value to null in your method like `, [FromQuery]string fio = null`. Also, make sure your route enable optionality as described here: https://stackoverflow.com/a/38249991/5107490. Also, Follow your logic in the inner methods to make sure it can handle null value. – ShayD Feb 09 '23 at 12:45