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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

it's still not working, message in the console says ""The fio field is required.""
It seem like your api requiring the field. Can you share your server code?
yes, this is api 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); }
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: stackoverflow.com/a/38249991/5107490. Also, Follow your logic in the inner methods to make sure it can handle null value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.